我正在尝试使用Google应用脚本从表单中获取信息,但我认为检查并查看表单是否已附加目标电子表格是一个好主意。如果是的话,我想抓住ID,如果不是,我想创建一个。每次我运行下面的代码时,de-bugger会抛出一个错误,告诉我“表单当前没有响应目的地。”
请帮忙
var form = FormApp.getActiveForm();
var formName = form.getTitle();
function randomQuestion() {
var theFormID = form.getId();
var sheetID = function sheetID() {
if ( form.getDestinationId() === 'undefined' ) {
var ss = SpreadsheetApp.create(formName + ' (Responses)');
form.setDestination(FormApp.DestinationType.SPREADSHEET, ss.getId());
return form.getDestinationId();
}else {
return form.getDestinationId();}
}
var sheet = sheetID();
g; // <-un-comment this to make the de-bugger throw an error =)
}
答案 0 :(得分:2)
以下是我用来测试是否有响应电子表格以及是否创建响应电子表格的代码。
var form = FormApp.getActiveForm();
var formName = form.getTitle();
var ssID;
try {
ssID = form.getDestinationId();
}
catch(e) {
var exception = e.name;
if (exception === "Exception" ){
var ss = SpreadsheetApp.create(formName + ' (Responses)');
form.setDestination(FormApp.DestinationType.SPREADSHEET, ss.getId());
ssID = form.getDestinationId();
}else{
ssID = form.getDestinationId();
}
}
答案 1 :(得分:1)
当表单没有响应目标时,方法getDestinationId()
会抛出异常。您需要使用try...catch处理此问题,例如:
...
var form = FormApp.getActiveForm(), idResponse;
try {
idResponse = form.getDestinationId();
}
catch(e) {
// TODO
}
...