2

我在 Google Docs for Spreadsheets(应用程序脚本)中构建了一段代码,用于将记录从一个电子表格传输到另一个电子表格。

function myFunction() {
    // Get Spreadsheets
    var source = SpreadsheetApp.openById("spreadsheetKeySource");
    var target = SpreadsheetApp.openById("spreadsheetKeyTarget");

    // Set Sheets
    var source_sheet = source.getSheetByName("Sheet1");
    var target_sheet = target.getSheetByName("Sheet1");

    // Get target last row
    var last_row = target_sheet.getLastRow();

    // Set Ranges
    var source_range = source_sheet.getRange("A1:B1");
    var target_range = target_sheet.getRange("A"+(last_row+1)+":B"+(last_row+1));

    // Fetch values
    var values = source_range.getValues();

    // Save to spreadsheet
    target_range.setValues(values);
}

我需要知道是否可以从源中获取内容并循环访问它,并且只添加目标中不存在的记录。显然,此功能不足以达到此目的,我希望有人可以将我推向正确的方向,甚至提供代码片段作为示例。到目前为止,我在谷歌或这里找不到任何有用的东西...... :(

谢谢你的帮助

编辑

我能够完成这个。范围发生了变化,现在我必须从具有多个工作表的多个电子表格中获取数据,以仅获取电子邮件并将它们添加到主电子邮件电子表格中。这是我创建的代码,希望它对其他人也有帮助。

我实际上是在打开 masterSpreadsheet 并添加了一个菜单按钮来运行此脚本时运行此脚本。

/**
* Updates the master spreadsheet with the set source spreadsheets and pages
**/
function updateMasterSpreadsheet() {
  //  var sourceSpreadsheetsArray = new Array();
  var sourceSpreadsheetsObject = new Object;

  // Add Spreadsheet IDs and sheet names that need to be copied from
  sourceSpreadsheetsObject['sourceSpreadsheet1Key'] = new Array("Sheet1");
  sourceSpreadsheetsObject['sourceSpreadsheet2Key'] = new Array("Sheet1");

  // Open master spreadsheet
  var target = SpreadsheetApp.openById("masterSpreadsheetKey");

  // Open master sheet
  var target_sheet = target.getSheetByName("Sheet1");

  // Loop through all source spreadsheets
  for (var id in sourceSpreadsheetsObject) {
    // Open source spreadsheet
    var source = SpreadsheetApp.openById(id);

    // Loop through and process each sheet
    for (var sheetID in sourceSpreadsheetsObject[id]) {
      // Open source spreadsheet sheet
      var source_sheet = source.getSheetByName(sourceSpreadsheetsObject[id][sheetID]);

      // Process sheet
      _updateMasterSpreadsheet(source_sheet, target_sheet);
    } // END - source sheets loop 

  } // END - source spreadsheets loop

  // Add last updated
  target_sheet.getRange("B1").setValue(new Date());
}

/**
* Performs the actual copy into the master spreadsheet
**/
function _updateMasterSpreadsheet(source_sheet, target_sheet) {
  // Get target last row
  var last_row = target_sheet.getLastRow();  

  // Get Source Range
  var source_range = source_sheet.getDataRange();

  // Fetch Source Values
  var source_data = source_range.getValues();  

  //Iterate over all cells, looking for non-empty ("") cells
  for (var row in source_data) {

    // check if empty
    if (source_data[row][0]!= "") {
      // Fetch data in the sheet (do this in the loop to ensure that we check all newly added items)
      var target_range = target_sheet.getDataRange();
      var target_data = target_range.getValues();  

      // Set flags
      var found = false;
      var stop = false;

      // Process loop
      while(found == false && stop == false) {
        // Check for duplicates
        for (var tmpRow in target_data) {
          if (source_data[row][0] == target_data[tmpRow][0]) {
            found = true;
            break;
          }
        }

        // If no duplicate, add to sheet
        if (!found) {
          last_row++;
          target_sheet.getRange("A"+last_row).setValue(source_data[row][0]);
        }

        // Prevent infinite loop
        stop = true;
      }
    }
  }
}
4

2 回答 2

1

Google Apps 脚本文档中的本教程应该可以帮助您:

教程:删除电子表格中的重复行

Apps 脚本库中还有一个名为“删除重复项”的脚本

于 2012-05-24T11:29:39.020 回答
0

One of our more experience people (which is plenty) may have a different take on it, but I'd guess you'd have to compare the two line-by-line. That would work only assuming the contents haven't been manually changed in either sheet.

What is it you're attempting to do? If you describe your use case, then we may be able to provide other thoughts...

于 2012-05-23T20:25:43.010 回答