一个Google Spreadsheet有多个工作表,其中一些链接到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;
}
答案 0 :(得分: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]