如何使用应用脚本指定 Google 表单目标的电子表格标签页名称



我正在使用Google脚本编辑器,并且基于Google表格创建了一个表单。 我已经根据从工作表中选择的当前单元格为表单上的某些问题指定了帮助文本。 然后,我将表单结果的目的地设置为不同的Google表格。 我想根据当前工作表中的当前单元格指定选项卡的名称。 这可能吗? 这是我现在拥有的:

var DestinationSheet = SpreadsheetApp.openByUrl('URL here');
form.setDestination(FormApp.DestinationType.SPREADSHEET, DestinationSheet.getId(););

但是我如何指定 TAB 名称,因为现在它只是每次都给它一个带有新数字的通用名称,即"表单响应 5"。

我想我可以以某种方式包含这个:DestinationSheet.insertSheet("currentcell");但这只是插入了另一张名为"currentcell"的工作表。

但是我如何指定 TAB 名称,因为现在它每次都给它一个带有新数字的通用名称,即"表单响应 5"。

目前无法设置工作表名称。但您可以重命名"Form Responses 5"工作表。

片段:

var DestinationSheet = SpreadsheetApp.openByUrl('URL here');
form.setDestination(FormApp.DestinationType.SPREADSHEET, DestinationSheet.getId(););
const formUrl = form.getEditUrl();
DestinationSheet
.getSheets()
.forEach(function(sheet){
var sheetUrl = sheet.getFormUrl()
//TODO Check format of both urls
if (sheetUrl  === formUrl) 
sheet.setName(DestinationSheet.getCurrentCell().getValue());//Function should be called from menu/button
})

引用:

  • Sheet#getFormUrl
  • 表单#getEditUrl

现在表单 url 后缀为/edit/viewport,您必须将其删除才能正确比较。我最终得到了这个解决方案:

function connectCurrentSpreadsheet(form, sheetName) {
const spreadsheet = SpreadsheetApp.getActive();
form.setDestination(FormApp.DestinationType.SPREADSHEET, spreadsheet.getId());
// rename spreadsheet
const formUrl = form.getEditUrl().replace('/edit', '');
spreadsheet.getSheets().forEach(sheet => {
const destFormUrl = sheet.getFormUrl().replace('/viewform', '');
if(destFormUrl === formUrl) {
sheet.setName(sheetName);
}
});
}

相关内容

最新更新