使用谷歌应用程序脚本从谷歌表更新多个谷歌表单

时间:2021-01-05 16:57:09

标签: google-apps-script google-sheets google-forms

我有一个用于更新一个表单的代码,但我希望它一次更新多个表单。 有人可以帮忙提供确切的代码 - 我是新手。

function getDataFromGoogleSheets(){
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName("Operators")
  const [header, ...data] = sheet.getDataRange().getDisplayValues();
  const choices = {}
  header.forEach(function(title, index) {
    choices[title] = data.map(row => row[index]).filter(e => e !== "");
  });
  return choices;
}

function populateGoogleForms() {
  const GOOGLE_FORM_ID = "1OudibtmcHOTBwQCObXu1zNqHrW_1p99FuyCmw5MJdpc";
  const googleForm = FormApp.openById(GOOGLE_FORM_ID)
  const items = googleForm.getItems();
  const choices = getDataFromGoogleSheets();
  items.forEach(function(item) {
    const itemTitle = item.getTitle();
    if (itemTitle in choices) {
      const itemType = item.getType();
      switch (itemType) {
        case FormApp.ItemType.CHECKBOX:
          item.asCheckboxItem().setChoiceValues(choices[itemTitle]);
          break;
        case FormApp.ItemType.LIST:
          item.asListItem().setChoiceValues(choices[itemTitle]);
          break;
        case FormApp.ItemType.MULTIPLE_CHOICE:
          item.asMultipleChoiceItem().setChoiceValues(choices[itemTitle]);
          break;
        default:
          Logger.log("Ignore question", itemTitle);
      }
    }
  });
  SpreadsheetApp.getActiveSpreadsheet().toast("Google Form Updated");
}

0 个答案:

没有答案