表单回复 spreadsheet.getSheets() 不 return 回复 sheet?

Form responses spreadsheet.getSheets() doesn't return responses sheet?

我的总体目标是每周为一个表单创建一个新的 'responses' sheet(在同一个 Spreadsheet 中)。在处理一周的回复结束时,考虑到 Form Responses Spreadsheets 的限制(无法删除行),我尝试执行以下操作:

  // ...

  // Copy the existing answers to a new sheet in the spreadsheet and rename it
  // with this week's DateTime.
  today = Utilities.formatDate(new Date(), 'GMT', 'dd/MM/yyyy HH:mm');
  currentSheet.copyTo(spreadsheet).setName(today);

  // Delete the form responses from the Form. This will _not_ update the
  // responses Spreadsheet.
  form = FormApp.openByUrl(spreadsheet.getFormUrl());
  form.deleteAllResponses();

  // Begin dodgy hack
  // First unlink the Form from the responses Spreadsheet
  form.removeDestination();

  // Delete the old responses Sheet
  console.log(`Removing the ${CURRENT_SHEET_NAME} sheet`);
  spreadsheet.deleteSheet(currentSheet);

  // Relink the existing responses Spreadsheet to the Form
  form.setDestination(FormApp.DestinationType.SPREADSHEET, SPREADSHEET_ID);

  // It's not possible to get a handle on the sheet the responses will be put
  // into from Form or FormApp, so there are two choices: iterate through 
  // the sheets and find one with the correct FormUrl, or hope that for
  // all eternity, Google Apps Scripts will add new sheets at the zeroth index.
  const formId = form.getId();

  // **MADNESS**: only the renamed sheet comes back from getSheets. In the
  // Spreadsheet UI there is now two sheets: `Form Responses N` and `today`
  // where N is the ridiculous number of times I've tried to run this script
  // and Today is the current DateTime.
  spreadsheet.getSheets().forEach((sheet) => console.log(sheet.getName()));

  // Below is the intended, currently non-functional, behaviour
  const newCurrentSheet = spreadsheet.getSheets().find((sheet) => {
     return (url && url.indexOf(formId) > -1);
  });

  newCurrentSheet.setName(CURRENT_SHEET_NAME);

我试过以下方法,none 有效:

感觉 像一个错误,但也许有另一种方法可以在重新链接表单后获得 Spreadsheet 的 'refreshed' 视图?或者我错过了一些非常明显的东西(可能)。

很可能您的脚本没有收到新的表单响应 sheet,因为在添加 sheet 之后缺少 SpreadsheetApp.flush()