将数据从源 Sheet 复制到目标 Sheet IF 源 Sheet:目标 Sheet 中不存在列 A 数据

Copy Data from Source Sheet to Target Sheet IF SourceSheet: Column A data does not exists in Target Sheet

我想使用取自本网站其他线程的以下代码将数据从源 Sheet 复制到目标 Sheet。它对所需输出有何帮助。

我想将数据从源 sheet 复制到目标 sheet。源 sheet 数据正在动态变化,因此,如果源 sheet 列 A 数据不存在于目标 sheet 列 A.[=15= 中,我想复制源 sheet 行]

请帮我修改一下代码

const ss = SpreadsheetApp.getActiveSpreadsheet();
const sourceSheet = ss.getSheetByName("Source");
const targetSheet = ss.getSheetByName("Target");
const startRow = 2; 
const APPENDED = 'APPENDED';

function appendToRecords() {
  const numRows = sourceSheet.getLastRow() - 1; // Number of rows to process
  const dataRange = sourceSheet.getRange(startRow, 1, numRows, sourceSheet.getLastColumn()); // Fetch the range of cells being used
  const sourceData = dataRange.getValues(); // Fetch values for each row in the Range.
  const lastColumn = sourceData[0].length;

//if (dataRange > 2) {
  for (var i = 0; i < sourceData.length; ++i) {
    var row = sourceData[i];
    if (row[1]=="" && row[lastColumn-1] ==APPENDED) { 
      sourceSheet.getRange(startRow + i, 9 ).setValue("");
    }
    else if (row[lastColumn-1] != APPENDED) {
      ///you should not do this line by line, but all at oncw with an array
      //row[lastColumn-1] = APPENDED; 
      row[8] = APPENDED;                         // to avoid incremental appending
      var lastRow = targetSheet.getLastRow();
      targetSheet.getRange(lastRow + 1, 1, 1, row.length).setValues([row]);
     sourceSheet.getRange(startRow + i, 9 ).setValue("APPENDED");  
    }
  }
}

样本数据sheet https://docs.google.com/spreadsheets/d/13NGHmUjnISK76wri8x9PoyU34KPLdW2TrOXVvoY5kuM/edit#gid=2100307022

问题:

  • 您想从源sheet复制目标数据中未找到 A 列的所有行。

解决方案:

  • 根据是否在目标 sheet 的任何行中找到列 A 值来过滤源行,使用 filter and some.
  • 通过 Range.setValues.
  • 将过滤后的行添加到目标 sheet

代码示例:

const ss = SpreadsheetApp.getActiveSpreadsheet();
const sourceSheet = ss.getSheetByName("Source");
const targetSheet = ss.getSheetByName("Target");
const startRow = 2; 
const CHECK_COLUMN = 1; // Column A

function appendToRecords() {
  const numRows = sourceSheet.getLastRow() - 1; // Number of rows to process
  const numCols = sourceSheet.getLastColumn();
  const lastTargetRow = targetSheet.getLastRow();
  const dataRange = sourceSheet.getRange(startRow, 1, numRows, sourceSheet.getLastColumn()); // Fetch the range of cells being used
  const sourceData = dataRange.getValues(); // Fetch values for each row in the Range.
  let targetData = [];
  if (lastTargetRow > 1) targetData = targetSheet.getRange(startRow, 1, lastTargetRow-startRow+1, numCols).getValues();
  const newData = sourceData.filter(sourceRow => {
    const columnA = sourceRow[CHECK_COLUMN-1];
    return !targetData.some(targetRow => targetRow[CHECK_COLUMN-1] === columnA);
  });
  if (newData.length) {
    targetSheet.getRange(lastTargetRow+1, 1, newData.length, newData[0].length).setValues(newData);
  }
}

上面的脚本假定目标 sheet 中有一个 header 行。如果不是这种情况,请替换:

if (lastTargetRow > 1) targetData = targetSheet.getRange(startRow, 1, lastTargetRow-startRow+1, numCols).getValues();

有了这个:

if (lastTargetRow > 0) targetData = targetSheet.getRange(1, 1, lastTargetRow, numCols).getValues();

注:

避免使用 setValue in a loop, this slows down your script a lot. See Use batch operations.