不同的Google表单网址(如何在大型电子表格中查找链接的“表单响应”表)?

问题描述 投票:0回答:1

一个Google电子表格有几张,其中一些与Google表格相关联。我试图使用this example,但我找不到链接表。在一种情况下,它显示没有任何域的表单的链接,但是sheet.getFormUrl()返回带有域的链接和另一个表单的ID!当我使用该链接时,它在第一个链接(没有域名)的浏览器中被替换。在这种情况下如何找到链接表和为什么链接不同?!

我有onFormSubmit事件处理程序(在窗体端):

/** 
 *  This function is retrieved by onFormSubmit Trigger on the Form side (not on Spreadsheet side)
 * 
 *  https://stackoverflow.com/questions/53377027/how-do-i-enable-permissions-to-formapp
 *  View / Show Manifest file:
 *  Add:
 *    "oauthScopes": [
 *      "https://www.googleapis.com/auth/forms",
 *      "https://www.googleapis.com/auth/spreadsheets"
 *    ]
 */
function onFormSubmit(event) {
  try {
    Logger.log(JSON.stringify(event));

    const sheet = getLinkedSheet(event);
    if(sheet) {
      Logger.log(sheet.getName());
    }

  } catch (err) {
    Logger.log(err.toString());
  }
}

function getLinkedSheet(event) {
  // Get the form to which this script is bound.
  //var form = FormApp.getActiveForm();
  //OR:
  var form = event.source; 
  var destinationId = form.getDestinationId();

  //No domain: https://docs.google.com/forms/d/e/**[ID1]**/viewform <------------- A
  var formURL = form.getPublishedUrl(); 

  var formID = form.getId();

  Logger.log("Form's URL:" +formURL);

  var ss = SpreadsheetApp.openById(destinationId);

  var sheets = ss.getSheets();  
  var match = null;
  for(var i=0; i<sheets.length; i++) {  

    //With domain: https://docs.google.com/a**/[DOMAIN]**/forms/d/**[ID2]**/viewform <----------- B
    Logger.log(JSON.stringify( sheets[i].getFormUrl() ) ); 

    if(sheets[i].getFormUrl() == formURL) {
      match = sheets[i]; //<----------------------- NO MATCHES FOUND
    }
  }

  Logger.log(JSON.stringify(match)); //null
  return match;
}
google-apps-script google-form
1个回答
2
投票

我有同样的问题,并在https://issuetracker.google.com/issues/128732931提交了一份错误报告。

为了解决这个问题,我打开每个工作表的formUrl的表单,然后检查并比较表单ID:

var form = FormApp.getActiveForm();
var formId = form.getId();
const matches = spreadSheet.getSheets().filter(function (sheet) {
  var sheetFormUrl = sheet.getFormUrl();
  if (sheetFormUrl){
    return FormApp.openByUrl(sheetFormUrl).getId() === formId;
  }
});
const sheet = matches[0]
© www.soinside.com 2019 - 2024. All rights reserved.