【问题标题】:How to specify the spreadsheet tab name of a Google form destination using Apps Script如何使用 Apps 脚本指定 Google 表单目标的电子表格选项卡名称
【发布时间】:2019-08-15 14:50:07
【问题描述】:

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

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

但是我如何指定 TAB 名称,因为现在它只是给它一个通用名称,每次都带有一个新数字,即“Form Responses 5”。

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

【问题讨论】:

    标签: google-apps-script google-sheets google-forms


    【解决方案1】:

    但是我如何指定 TAB 名称,因为现在它只是给它一个通用名称,每次都带有一个新数字,即“Form Responses 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
        })
    

    参考资料:

    【讨论】:

    • 这似乎对我不起作用。该表格仍被命名为“Form Responses XX”。 XX 是一个随机数。
    • @Amy 显示sheetUrlformUrl 的值
    • 它不起作用,因为 .getSheets() 在执行期间不包含新工作表。它确实会在下次执行脚本时列出工作表,但这没有帮助。我猜您需要在 .setDestination(...) 之后等待创建新工作表。不知道该怎么做。
    • @JamesTalmage 在.getSheets() 行之前添加SpreadsheetApp.flush()
    • 这对我也不起作用,我不明白它应该如何起作用。 sheetUrl 和 formUrl 返回用于编辑和查看表单的 URL。它们与在目标表中创建的表单响应选项卡无关,据我所知,此脚本 sn-p 不会以任何方式与该选项卡交互。 OP 询问如何在与 setDestination 相同的步骤中指定选项卡名称。即使不能在同一步骤中完成,如果我们可以获取新创建的 Tab 的名称或 ID 应该是可行的。我没有看到问题已得到回答。
    【解决方案2】:

    现在表单 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);
        }
      });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-13
      • 2020-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多