我试图建立一个脚本,创建一个基于表(工作)的表单,然后我希望它提交响应到一个特定的表,所以我可以有脚本与它交互,手动连接表单到表将不实用,因为多个表单可以每天创建
当我运行脚本时,出现这个错误代码
异常:获取对象FormApp.Form的setDestination方法或属性时出现意外错误。
下面是我的代码,如有帮助将不胜感激
function bug() {
var sheet = SpreadsheetApp.getActive().getSheetByName('TC');
var value = sheet.getRange("A3").getValue();
var em = sheet.getRange("B3").getValue();
var cos = sheet.getRange("E3").getValue();
var name = sheet.getRange("D3").getValue();
var sup = sheet.getRange("C3").getValue();
var form = FormApp.create(value);
var item = form.addMultipleChoiceItem();
item.setHelpText("Name: " + name +
"nnEmail: " + em +
"nnQuote No: " + value +
"nnProject: " + sup +
"nnTotal Cost: " + cos +
"nnBy selecting approve you agree to the cost and timeframe specified by the quote and that the details above are correct")
.setChoices([item.createChoice('Approve'), item.createChoice('Deny')]);
var item = form.addCheckboxItem();
item.setTitle('What extras would you like to add on?');
item.setChoices([
item.createChoice('Damp-Course'),
item.createChoice('Wrap'),
item.createChoice('Taco')
]);
Logger.log('Published URL: ' + form.getPublishedUrl());
Logger.log('Editor URL: ' + form.getEditUrl());
Logger.log('ID: ' + form.getId());
var ssId = sheet.getSheetId();
form.setDestination(FormApp.DestinationType.SPREADSHEET, ssId);
var SendTo = "insertgenertic@email.com";
var link = form.getPublishedUrl();
var message = "Please follow the link to accept you Quotation " + form.getPublishedUrl();
//set subject line
var Subject = 'Quote' + value + 'Confirmation';
const recipient = em;
const subject = "Confirmation of order"
const url = link
GmailApp.sendEmail(recipient, subject, message);
}
不能向先前存在的工作表发送表单响应。当您将表单链接到目标电子表格时,无论您是否使用Apps Script设置目标,它都会创建一个新工作表来存储这些响应。您只能选择是使用现有的电子表格还是新建的电子表格。
因此,在调用Form时必须提供电子表格id。setDestination(类型、id)。由于您提供的是工作表id,因此您将得到一个错误。
你应该这样做:
function bug() {
// ...
const spreadsheetId = SpreadsheetApp.getActive().getId();
form.setDestination(FormApp.DestinationType.SPREADSHEET, spreadsheetId);
// ...
}
相关:
- 链接Google表单响应到已经存在的Google电子表格选项卡
- 选择保存表单响应的位置
form.setDestination(FormApp.DestinationType.SPREADSHEET, ssId);
ssId应该是电子表格的id,而不是工作表的id
例子表格id
getSheetId