Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.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

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,我为GoogleSheets编写了这个脚本,并希望在同一个工作表中使用它两次。当我把它们放在工作表的同一脚本部分时,它只运行其中一个,而不是两个 以下是我一直在编写的脚本: var sourceSpreadsheetID = "INPUT1";//these are global Cooper Edit var sourceWorksheetName = "INPUT1"; var targetSpreadsheetID = "INPUT1"; var targetWorksheetName =

我为GoogleSheets编写了这个脚本,并希望在同一个工作表中使用它两次。当我把它们放在工作表的同一脚本部分时,它只运行其中一个,而不是两个

以下是我一直在编写的脚本:

var sourceSpreadsheetID = "INPUT1";//these are global Cooper Edit
var sourceWorksheetName = "INPUT1";
var targetSpreadsheetID = "INPUT1";
var targetWorksheetName = "INPUT1";

function importData1() {
  var thisSpreadsheet = SpreadsheetApp.openById(sourceSpreadsheetID);
  var thisWorksheet = thisSpreadsheet.getSheetByName(sourceWorksheetName);
  var thisData = thisWorksheet.getDataRange().getValues();
  var toSpreadsheet = SpreadsheetApp.openById(targetSpreadsheetID);
  var toWorksheet = toSpreadsheet.getSheetByName(targetWorksheetName);
  toWorksheet.clearContents();
  toWorksheet.getRange(1, 1, thisData.length, thisData[0].length).setValues(thisData); 
}



    var sourceSpreadsheetID = "INPUT2";//these are global Cooper Edit
    var sourceWorksheetName = "INPUT2";
    var targetSpreadsheetID = "INPUT2";
    var targetWorksheetName = "INPUT2";

    function importData2() {
      var thisSpreadsheet = SpreadsheetApp.openById(sourceSpreadsheetID);
      var thisWorksheet = thisSpreadsheet.getSheetByName(sourceWorksheetName);
      var thisData = thisWorksheet.getDataRange().getValues();
      var toSpreadsheet = SpreadsheetApp.openById(targetSpreadsheetID);
      var toWorksheet = toSpreadsheet.getSheetByName(targetWorksheetName);
      toWorksheet.clearContents();
      toWorksheet.getRange(1, 1, thisData.length, thisData[0].length).setValues(thisData); 
    }

Cooper Edit:每次运行每个函数时都会运行全局声明。

如果希望通过一次调用同时运行这两个函数,请添加一个中间人,该中间人将调用这两个函数。例如

function middleman(){
  importData1();
  importData2();
}

查看我的编辑。解决方案是将所有全局声明放在函数声明中,您的问题就会消失。啊,好的!那么,仅仅添加{///这些是全局的}会有帮助吗?只是想确保我明白!!!谢谢你,库佩里,我不知道你是否明白。我在你的问题中标记的那些在函数声明之外的行是全局的,它们在每次调用函数时都会得到求值,而你的方式意味着每次调用使用它们的函数时,它们都会使用INPUT2,因为这是文件中的最后一个。因此,我建议您将它们放在函数声明中。啊,好的,是的,我现在明白了这是一件不同于客户端的事情,在客户端的情况下,全局参数可以由函数更改。等等,我将把它放在哪里,以结合2?