Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 要从一张图纸复制到另一张图纸的脚本,需要编辑_Google Apps Script_Google Sheets - Fatal编程技术网

Google apps script 要从一张图纸复制到另一张图纸的脚本,需要编辑

Google apps script 要从一张图纸复制到另一张图纸的脚本,需要编辑,google-apps-script,google-sheets,Google Apps Script,Google Sheets,我有这个脚本,这是工作得很好,但我需要编辑它到 a) 仅返回自上次运行以来的新行 b) 只返回某些单元格而不是整行 任何指导都将不胜感激 function Copy() { var sourceSheet = SpreadsheetApp.openById('1WAtRDYhfVXcBKQoUxfTJORXwAqYvVG2Khl4GuJEYSIs') .getSheetByName('Jobs Log'); var range = sourceSheet.getRange(1, 1, s

我有这个脚本,这是工作得很好,但我需要编辑它到 a) 仅返回自上次运行以来的新行 b) 只返回某些单元格而不是整行

任何指导都将不胜感激

function Copy() {
var sourceSheet = SpreadsheetApp.openById('1WAtRDYhfVXcBKQoUxfTJORXwAqYvVG2Khl4GuJEYSIs')
    .getSheetByName('Jobs Log');
var range = sourceSheet.getRange(1, 1, sourceSheet.getLastRow(), sourceSheet.getLastColumn());
var arr = [];
var rangeval = range.getValues()
    .forEach(function (r, i, v) {
        if (r[1] == 'Amber') arr.push(v[i]);
    });
var destinationSheet = SpreadsheetApp.openById('137xdyV8LEh6GAhAwSx4GmRGusnjsHQ0VGlWbsDLXf2c')
    .getSheetByName('Sheet1');
destinationSheet.getRange(destinationSheet.getLastRow() + 1, 1, arr.length, arr[0].length)
    .setValues(arr); 

}

为了只检查上次运行后添加的新数据,我们必须在属性中存储
.getLastRow()
值,并在每次运行时检索它。我们还必须在几个假设下工作:

  • 在输入数据中,新值仅附加在底部,而不会插入其他数据之间
  • 永远不会从输入工作表中删除数据(如果忽略此项,则还必须为删除数据后运行的最后一行创建更新脚本)
  • 在添加新数据后但在运行此脚本之前,不会对工作表进行排序
  • 所以你需要一些类似于

    var sourceSheet = SpreadsheetApp.openById('1WAtRDYhfVXcBKQoUxfTJORXwAqYvVG2Khl4GuJEYSIs')
        .getSheetByName('Jobs Log');
    var lastRow = sourceSheet.getLastRow();
    
    // note that you need to hav the script property initialized and stored
    // or adjust the if to also check if prevLastRow gets a value
    var prevLastRow = PropertiesService.getScriptProperties().getProperty('lastRow')
    
    if (lastRow <= prevLastRow) {
      return; // we simply stop the execution right here if we don't have more data
    }
    // then we simply start the range from the previous last row
    // and take the amount of rows added afterwards
    var range = sourceSheet.getRange(prevLastRow,
                                     1,
                                     lastRow - prevLastRow,
                                     sourceSheet.getLastColumn()
                                    ); 
    
    变为

    if (r[1] == 'Amber') arr.push([v[i][0], v[i][3], v[i][2]]);
    
    它将为每行输出
    adc
    列(按该顺序)

    最后,在脚本结束之前最不需要运行的是

    PropertiesService.getScriptProperties().setProperty('lastRow', lastRow)
    

    这将让我们知道下次运行脚本时停止的位置。同样,请记住,只有当新数据始终位于新行中时,这种方法才有效。否则,您需要使用另一种方法并检索2个数据数组。1用于整个输入表,1用于输出表。如果检查,则必须执行2次
    。第一个用于查看是否满足您的条件,第二个用于查看输出数据中是否已经存在该条件。

    为了只检查上次运行后添加的新数据,我们必须在属性中存储
    .getLastRow()
    值,并在每次运行时检索该值。我们还必须在几个假设下工作:

  • 在输入数据中,新值仅附加在底部,而不会插入其他数据之间
  • 永远不会从输入工作表中删除数据(如果忽略此项,则还必须为删除数据后运行的最后一行创建更新脚本)
  • 在添加新数据后但在运行此脚本之前,不会对工作表进行排序
  • 所以你需要一些类似于

    var sourceSheet = SpreadsheetApp.openById('1WAtRDYhfVXcBKQoUxfTJORXwAqYvVG2Khl4GuJEYSIs')
        .getSheetByName('Jobs Log');
    var lastRow = sourceSheet.getLastRow();
    
    // note that you need to hav the script property initialized and stored
    // or adjust the if to also check if prevLastRow gets a value
    var prevLastRow = PropertiesService.getScriptProperties().getProperty('lastRow')
    
    if (lastRow <= prevLastRow) {
      return; // we simply stop the execution right here if we don't have more data
    }
    // then we simply start the range from the previous last row
    // and take the amount of rows added afterwards
    var range = sourceSheet.getRange(prevLastRow,
                                     1,
                                     lastRow - prevLastRow,
                                     sourceSheet.getLastColumn()
                                    ); 
    
    变为

    if (r[1] == 'Amber') arr.push([v[i][0], v[i][3], v[i][2]]);
    
    它将为每行输出
    adc
    列(按该顺序)

    最后,在脚本结束之前最不需要运行的是

    PropertiesService.getScriptProperties().setProperty('lastRow', lastRow)
    

    这将让我们知道下次运行脚本时停止的位置。同样,请记住,只有当新数据始终位于新行中时,这种方法才有效。否则,您需要使用另一种方法并检索2个数据数组。1用于整个输入表,1用于输出表。如果
    检查,则必须执行2次
    。第一个用于查看是否满足您的标准,第二个用于查看输出数据中是否已经存在该标准。

    forEach编辑工作非常出色,我衷心感谢。属性服务编辑不起作用-可能是因为我必须先设置一个值,然后才能获取它,并且正如所写的,该集位于底部…因此第一次运行将无法工作,因为没有设置任何内容。我仍在学习PropertyService,所以我还不清楚如何进行初始设置。错误是:“在对象函数getScriptProperties(){/**/}.(第7行,文件“Code”)”中找不到函数getProperty,它指的是“var prevLastRow=PropertiesService.getScriptProperties.getProperty('lastRow')”@joanna对此表示抱歉,忘记括号了。它是
    PropertiesService.getScriptProperties()
    ,然后是
    .getProperty()
    .setProperty()
    。虽然谷歌脚本确实有自动完成功能,所以你可以自己捕捉到:)编辑answer@joanna如果答案帮助您解决这个问题,请考虑将它标记为正确的答案,在我进行了脚本属性更正之后,它开始在第15行“不能找到方法GeTrange(空,数,数,数)”上产生新的错误,我不明白,我不想一直打扰你,所以我放弃了它,重新开始。新剧本很管用。我肯定这是我做错了什么,所以我会帮你改正的。再次感谢!forEach编辑的作品非常漂亮,我衷心的感谢。属性服务编辑不起作用-可能是因为我必须先设置一个值,然后才能获取它,并且正如所写的,该集位于底部…因此第一次运行将无法工作,因为没有设置任何内容。我仍在学习PropertyService,所以我还不清楚如何进行初始设置。错误是:“在对象函数getScriptProperties(){/**/}.(第7行,文件“Code”)”中找不到函数getProperty,它指的是“var prevLastRow=PropertiesService.getScriptProperties.getProperty('lastRow')”@joanna对此表示抱歉,忘记括号了。它是
    PropertiesService.getScriptProperties()
    ,然后是
    .getProperty()
    .setProperty()
    。虽然谷歌脚本确实有自动完成功能,所以你可以自己捕捉到:)编辑answer@joanna如果答案帮助您解决这个问题,请考虑将它标记为正确的答案,在我进行了脚本属性更正之后,它开始在第15行“不能找到方法GeTrange(空,数,数,数)”上产生新的错误,我不明白,我不想一直打扰你,所以我放弃了它,重新开始。新剧本很管用。我肯定这是我做错了什么,所以我会帮你改正的。再次感谢!