简体   繁体   English

Append 最后一个空行上的新记录(而不是忽略最后一个空行并在工作表末尾添加新行)

[英]Append new records on the last empty row (instead of ignoring last empty rows and adding the new row at the end of the sheet)

I have this script it works ok.我有这个脚本,它工作正常。 But, it ignores the blank rows after the last record row in the sheet.但是,它会忽略工作表中最后一个记录行之后的空白行。 Instead it adds a whole new row to the end of the sheet for the new record that is being appended.相反,它将一个全新的行添加到要附加的新记录的工作表的末尾。 For example: I have a sheet with 1000 rows.例如:我有一张 1000 行的工作表。 At this instance, there are only records up to row 25. So, what it does... it adds/ appends the new record at row 1001 instead of adding it to the empty row after the last record ie row 26.在这种情况下,只有第 25 行之前的记录。所以,它做了什么......它在第 1001 行添加/追加新记录,而不是将其添加到最后一条记录(即第 26 行)之后的空行。

Here is a dummy sheet link: https://docs.google.com/spreadsheets/d/1mt9G9PWdIvAQsQSWmRb14o8eeocCx9gkOWJT6KHAGa0/edit?usp=sharing这是一个虚拟表链接: https://docs.google.com/spreadsheets/d/1mt9G9PWdIvAQsQSWmRb14o8eeocCx9gkOWJT6KHAGa0/edit?usp=sharing

function appendUniqueRows() {
      var ss = SpreadsheetApp.getActive();
      var sourceSheet = ss.getSheetByName('source');
      var destSheet = ss.getSheetByName('destination');
    
      var sourceData = sourceSheet.getRange("A:D").getValues();
      var destData = destSheet.getRange("E:H").getValues();
    
      //Check whether destination sheet is empty
    if (destData.length === 1 && "" === destData[0].join('')) {
      // Empty, so ignore the phantom row
        destData = [];
      }
    
      // Generate hash for comparisons
      var destHash = {};
      destData.forEach(function(row) {
        destHash[row.join('')] = true; // could be anything
      });
    
      // Concatentate source rows to dest rows if they satisfy a uniqueness filter
      var mergedData = destData.concat(sourceData.filter(function (row) {
        var hashedRow = row.join('');
        if (!destHash.hasOwnProperty(hashedRow)) {
          // This row is unique
          destHash[hashedRow] = true;   // Add to hash for future comparisons
          return true;                  // filter -> true
        }
        return false;                   // not unique, filter -> false
      }));
    
      // // Check whether two data sets were the same width
      // var sourceWidth = (sourceData.length > 0) ? sourceData[0].length : 0;
      // var destWidth = (destData.length > 0) ? destData[0].length : 0;
      // if (sourceWidth !== destWidth) {
      //   // Pad out all columns for the new row
      //   var mergedWidth = Math.max(sourceWidth,destWidth);
      //   for (var row=0; row<mergedData.length; row++) {
      //     for (var col=mergedData[row].length; col<mergedWidth; col++)
      //       mergedData[row].push('');
      //   }
      // }
    
      // Write merged data to destination sheet
      destSheet.getRange(1, 5, mergedData.length, mergedData[0].length)
               .setValues(mergedData);
    }

I believe your goal as follows.我相信你的目标如下。

  • You want to add the filtered sourceData to the next row of the last row of destData .您想将过滤后的sourceData添加到destData最后一行的下一行。

I think that the reason of your issue is due to var mergedData = destData.concat(sourceData.filter(function (row) { . In this case, the filtered sourceData is added to destData of var destData = destSheet.getRange("E:H").getValues(); . destSheet.getRange("E:H").getValues() includes the empty rows. So, in this case, how about the following modification?我认为你的问题的原因是由于var mergedData = destData.concat(sourceData.filter(function (row) { 。在这种情况下,过滤后的sourceData被添加到 var destData var destData = destSheet.getRange("E:H").getValues(); . destSheet.getRange("E:H").getValues()包括空行。那么,在这种情况下,如何进行以下修改?

From:从:

var sourceData = sourceSheet.getRange("A:D").getValues();
var destData = destSheet.getRange("E:H").getValues();

To:至:

var sourceData = sourceSheet.getRange("A1:D" + sourceSheet.getLastRow()).getValues();
var destData = destSheet.getRange("E1:H" + destSheet.getLastRow()).getValues();

or或者

From:从:

var mergedData = destData.concat(sourceData.filter(function (row) {

To:至:

var mergedData = destData.filter(row => row.join('')).concat(sourceData.filter(function (row) {

Note:笔记:

  • When the process cost is considered, I think that the 1st modification will be suitable.考虑到工艺成本,我认为第一次修改比较合适。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM