Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.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
Javascript getSheets()谷歌应用程序脚本多次调用问题_Javascript_Google Apps Script_Google Sheets_Synchronization_Google Forms - Fatal编程技术网

Javascript getSheets()谷歌应用程序脚本多次调用问题

Javascript getSheets()谷歌应用程序脚本多次调用问题,javascript,google-apps-script,google-sheets,synchronization,google-forms,Javascript,Google Apps Script,Google Sheets,Synchronization,Google Forms,(为了这篇文章的目的,我将谷歌电子表格中的每一张“工作表”都称为一个标签,阅读时我发现命名惯例令人困惑) 在Google Sheets插件中,我试图创建一个设置过程,允许用户根据对该表单的提示输入Google表单的链接(我不只是手动将所有ID输入代码的原因是,所使用的表单本身可能会因用户而异),并将该表单的响应链接到电子表格,然后使用预先指定的名称重命名该选项卡 无论出于何种原因,每当我多次调用getSheets()函数时,调用的返回值都与第一次调用的返回值相同。例如,如果我有3个选项卡,名为“

(为了这篇文章的目的,我将谷歌电子表格中的每一张“工作表”都称为一个标签,阅读时我发现命名惯例令人困惑)

在Google Sheets插件中,我试图创建一个设置过程,允许用户根据对该表单的提示输入Google表单的链接(我不只是手动将所有ID输入代码的原因是,所使用的表单本身可能会因用户而异),并将该表单的响应链接到电子表格,然后使用预先指定的名称重命名该选项卡

无论出于何种原因,每当我多次调用
getSheets()
函数时,调用的返回值都与第一次调用的返回值相同。例如,如果我有3个选项卡,名为“first,second,third”,并且我运行了附加的代码,
sheetList
正确返回为
[first,second,third]
。但是,假设在
for
循环期间,添加了一个选项卡(作为链接表单的结果),然后我再次调用
getSheets()
以获取新的第一个选项卡(应该是链接表单数据),返回的仍然是
[first]

不确定从这里走到哪里,这是应用程序脚本的问题吗?还是我的方法?如有任何提示或建议,将不胜感激

    const separateSheetNames = [
    "English Household Information",
    "English Camper Information",
    "English Parent Signatures",
    "English Household Number Recovery",
    "Spanish Household Information",
    "Spanish Camper Information",
    "Spanish Parent Signatures",
    "Spanish Household Number Recovery"
  ];
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheetList = []
  ss.getSheets().forEach(function(val){sheetList.push(val.getName())});

  for (i=0;i<separateSheetNames.length;i++){
    var ui = SpreadsheetApp.getUi();
    var currentSheetName = separateSheetNames[i]
    var isAlreadySheet = sheetList.indexOf(currentSheetName)

    if (isAlreadySheet == -1){
      var formUrl = ui.prompt('Paste the link for the ' + currentSheetName + ' form').getResponseText()
      var form = FormApp.openByUrl(formUrl)
      const ssId = ss.getId();
      form.setDestination(FormApp.DestinationType.SPREADSHEET, ssId)
      var firstsheet = ss.getSheets()[0]
      console.log('firstSheetName ' + firstsheet.getName())
      firstsheet.setName(currentSheetName)
      var currentSheetLastCol = ss.getSheetByName(currentSheetName).getLastColumn()+1
      ss.getSheetByName(currentSheetName).getRange(1,currentSheetLastCol).setValue('Row in Combined Data')
    }
  }
const separateSheetNames=[
“英国家庭信息”,
“英语露营者信息”,
“英文家长签名”,
“英国家庭号码恢复”,
“西班牙家庭信息”,
“西班牙露营者信息”,
“西班牙家长签名”,
“西班牙家庭人数恢复”
];
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sheetList=[]
ss.getSheets().forEach(函数(val){sheetList.push(val.getName())});

对于(i=0;i将此问题留给可能遇到类似问题的任何其他人,@TheMaster提供的解决方案非常有效:


在调用
getSheets()
之前,请尝试
SpreadsheetApp.flush()

在调用
getSheets()
之前,请尝试
SpreadsheetApp.flush()
。@主人工作得很好,谢谢!