如果单元格包含公式;然后,将公式复制到目标 Sheet else 值或空白,原样

If Cell Contains formula; then, Copy Formula to Destination Sheet else value or blank , As it is

我有一个脚本可以帮助将数据从源 sheet(UserForm) 复制到目标 sheet(Datasheet),效果很好。现在我需要一个额外的任务,如果 E12 到 E22 有公式或值,就照原样复制。现在它只获得价值而不是公式,我需要两者。 例如。 E15 = C15*D15 和 E16 = 30,我需要复制公式和值。请使用以下脚本进行指导

function submitdata() {
  const dstSpreadsheetId = "1QL0jaNts2YRkZTlxmS0bk7V1fVVHBsJFmxS5C05PEmA"; // Please set the destination Spreadsheet ID (WB-DataSheet_B).
  const dstSheetName = "DataSheet";
  const srcSheetName = "UserForm";

  // Retrieve values from source sheet and create an array and search value.
  const srcSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  const ranges = ["C3", "C7", "C8", "D8", "D6", "D4", "E8", "E19", "E20", "E21", "E22", "B10", "C10", "D10", "E10", "B11", "C11", "D11", "E11", "B12", "C12", "D12", "E12", "B13", "C13", "D13", "E13", "B14", "C14", "D14", "E14", "B15", "C15", "D15", "E15", "B16", "C16", "D16", "E16", "B17", "C17", "D17", "E17", "B18", "C18", "D18", "E18", "B19", "C19", "B20", "C20", "B21", "C21", "B22", "C22","D19", "D20", "D21", "D22"];
  const [search, ...values] = Sheets.Spreadsheets.Values.batchGet(srcSpreadsheet.getId(),{ ranges: ranges.map(r => `'${srcSheetName}'!${r}`) }).valueRanges.flatMap(({ values, range }) => {
  if (range.includes("B10:E18")) return values ? values.flat() : [];
  if (range.includes("B19:D22")) return values ? values.flat() : [];
  return values ? values[0][0] : "";
});
if (!search) return;

  // Put the array to the destination sheet using the search value.
  const dstSheet = SpreadsheetApp.openById(dstSpreadsheetId).getSheetByName(dstSheetName);
  const range = dstSheet.getRange("A2:A" + dstSheet.getLastRow()).createTextFinder(search).findNext();
  if (range) {
    dstSheet.getRange(range.getRow(), 2, 1, values.length).setValues([values]);
  } else {
    dstSheet.getRange(dstSheet.getLastRow() + 1, 1, 1, values.length + 1).setValues([[search, ...values]]);
  }
}

还与实际数据(用户表单和数据sheet)共享了两个点差sheet供您参考https://docs.google.com/spreadsheets/d/1NY_ckzEWxU7DCGro5tTqzpiOi6iG5PAQFxpZg0OKodY/edit?usp=sharing

https://docs.google.com/spreadsheets/d/1QL0jaNts2YRkZTlxmS0bk7V1fVVHBsJFmxS5C05PEmA/edit?usp=sharing

你的情况,下面的修改怎么样?

发件人:

  const [search, ...values] = Sheets.Spreadsheets.Values.batchGet(srcSpreadsheet.getId(),{ ranges: ranges.map(r => `'${srcSheetName}'!${r}`) }).valueRanges.flatMap(({ values, range }) => {
  if (range.includes("B10:E18")) return values ? values.flat() : [];
  if (range.includes("B19:D22")) return values ? values.flat() : [];
  return values ? values[0][0] : "";
});

收件人:

const [search, ...values] = Sheets.Spreadsheets.Values.batchGet(srcSpreadsheet.getId(), { ranges: ranges.map(r => `'${srcSheetName}'!${r}`), valueRenderOption: "FORMULA" }).valueRanges.flatMap(({ values, range }) => {
  if (range.includes("B10:E18")) return values ? values.flat() : [];
  if (range.includes("B19:D22")) return values ? values.flat() : [];
  return values ? values[0][0] : "";
});
  • 本次修改,新增valueRenderOption: "FORMULA"。这样,当单元格有公式时,可以检索公式而不是显示值。

注:

  • 顺便说一句,作为附加信息,在你的情况下,例如当源sheet的“E22”有公式=SUM(E19:E21)时,检索到的公式是=SUM(E19:E21).因此,当将其放入目标单元格 sheet 时,将使用此公式。在这种情况下,我认为公式的结果值与源sheet的结果值不同。因为 sheet 的结构在源 sheet 和目标 sheet 之间是不同的。如果要为目标 sheet 更改此公式,我认为脚本会很复杂。请注意这一点。
    • 根据您 Now I need an additional task that if E12 to E22 has formula or value, copy as it is. now it gets only value but not formula, I need both. eg. E15 = C15*D15 and E16 = 30, I need to copy both formula and value. 的问题,我了解到您只是想要从源 sheet 到目标 sheet.
    • 的公式

参考: