Google apps script 谷歌脚本-可以';表单创建后找不到目标工作表

Google apps script 谷歌脚本-可以';表单创建后找不到目标工作表,google-apps-script,google-sheets,google-forms,Google Apps Script,Google Sheets,Google Forms,我的脚本以编程方式创建Google表单,并将目标设置为当前电子表格: var sheetId = SpreadsheetApp.getActiveSpreadsheet().getId(); var form = FormApp.create(acctName).setTitle(acctName).setDestination(FormApp.DestinationType.SPREADSHEET, sheetId); // several form items added here

我的脚本以编程方式创建Google表单,并将目标设置为当前电子表格:

var sheetId = SpreadsheetApp.getActiveSpreadsheet().getId();  
var form = FormApp.create(acctName).setTitle(acctName).setDestination(FormApp.DestinationType.SPREADSHEET, sheetId);

// several form items added here
接下来,我想重命名目标工作表并进行一些格式编辑。然而,当我这样做的时候,床单似乎并不存在。例如,
getSheets()
不包含新工作表!(是的,理想情况下,我希望通过ID打开工作表;请参阅下面的函数,该函数也不起作用。)

< >上面的代码打开了我将要考虑的索引1,而不是索引0,因为该表没有索引。我甚至尝试过创建几秒钟的延迟(希望允许客户端和服务器之间进行同步只是时间问题),但没有成功

我还尝试了以下类似的方法,正如这里关于堆栈溢出的其他地方所建议的那样,但是目标工作表在
getSheets()
中不会出现,即使是通过单独的函数调用:

function getFormDestinationSheetId(form) {

  var destinationId = form.getDestinationId();      
  if(destinationId) {

    var spreadsheet = SpreadsheetApp.openById(destinationId);        
    spreadsheet.getSheets().forEach(          
      function(sheet) {            
        var sheetFormUrl = sheet.getFormUrl();
        if(sheetFormUrl) {             
          var sheetForm = FormApp.openByUrl(sheetFormUrl);
          if(sheetForm.getId() == form.getId()) {
            return sheet.getSheetId();
          }
        }
      }
    );
  }
  return null;
}
我还没有在网上找到任何人有类似的问题。如有任何建议,将不胜感激。谢谢

欢迎来到Stack

我想你的脚本被绑定到一张纸上了?根据调用脚本的方式,由于浏览器缓存,您可能看不到新工作表

function myFunction() {
  var sheet = SpreadsheetApp.getActive();
  var sheetId = sheet.getId();  
  var form = FormApp.create('acctName').setTitle('acctName').setDestination(FormApp.DestinationType.SPREADSHEET, sheetId);

  var ssSheets = sheet.getSheets();

  var respSheet = ssSheets[0]

  respSheet.getRange('A1').setBackground('RED');
  respSheet.getRange('C1').setFormula('=COUNTA($C$2:$C)');
  respSheet.setColumnWidth(2, 100);

  SpreadsheetApp.flush();
}

我不熟悉这个API,但是
sheets[0]
不是第一个工作表的逻辑参考,因为数组(通常)是?撇开它不谈,总的来说,这是一篇写得很好的第一篇文章。您的问题是明确的,您的标记是适当的,并且您包含了相关的代码。谢谢你在这件事上花了这么多心思。希望你能从比我更熟悉谷歌API的人那里得到一个有见识的回复。请在打开电子表格后添加一个尝试添加
SpreadsheetApp.flush()
。如果您手动打开电子表格,是否会看到目标工作表?Jeremy。。。表〔0〕返回我所考虑的表〔1〕。感谢您的反馈。SpreadsheetApp.flush()FTW!!!现在似乎正在按预期工作。谢谢你,詹姆斯!
function myFunction() {
  var sheet = SpreadsheetApp.getActive();
  var sheetId = sheet.getId();  
  var form = FormApp.create('acctName').setTitle('acctName').setDestination(FormApp.DestinationType.SPREADSHEET, sheetId);

  var ssSheets = sheet.getSheets();

  var respSheet = ssSheets[0]

  respSheet.getRange('A1').setBackground('RED');
  respSheet.getRange('C1').setFormula('=COUNTA($C$2:$C)');
  respSheet.setColumnWidth(2, 100);

  SpreadsheetApp.flush();
}