Identifying Form destination (Spreadsheet AND SHEET)

前端 未结 4 1818
栀梦
栀梦 2020-12-10 22:21

I\'m working on a script that interacts with Google Form\' response sheet.

FormApp.getActiveForm().getDestinationId()

give me the spreadshe

4条回答
  •  猫巷女王i
    2020-12-10 22:51

    There is now a way to verify which sheet in a Google Sheets file with multiple linked forms corresponds to the current Form - through the use of Sheet#getFormUrl(), which was added to the Sheet class in 2017.

    function getFormResponseSheet_(wkbkId, formUrl) {
      const matches = SpreadsheetApp.openById(wkbkId).getSheets().filter(
        function (sheet) {
          return sheet.getFormUrl() === formUrl;
        });
      return matches[0]; // a `Sheet` or `undefined`
    }
    function foo() {
      const form = FormApp.getActiveForm();
      const destSheet = getFormResponseSheet_(form.getDestinationId(), form.getPublishedUrl());
      if (!destSheet)
        throw new Error("No sheets in destination with form url '" + form.getPublishedUrl() + "'");
      // do stuff with the linked response destination sheet.
    }
    

    If you have unlinked the Form and the destination spreadsheet, then obviously you won't be able to use getDestinationId or getFormUrl.

提交回复
热议问题