如何以编程方式打开新电子表格

abb*_*ood 2 javascript google-sheets google-apps-script

setActiveSpreadSheet文档中,示例非常清楚:

// The code below will make the spreadsheet with key "1234567890" the active spreadsheet
var ss = SpreadsheetApp.openById("1234567890");
SpreadsheetApp.setActiveSpreadsheet(ss);
Run Code Online (Sandbox Code Playgroud)

然而,在我的 Google 脚本中实现这一点,什么也没有发生(甚至没有错误消息)..想法?

更新

根据下面的答案,很明显上面的代码对 UI 没有影响。我尝试这样做:

function goToEstimate(sheetId)
{
  window.open('https://docs.google.com/spreadsheets/d/'+sheetId);
}
Run Code Online (Sandbox Code Playgroud)

但后来我得到了这个错误:

ReferenceError:“窗口”未定义。

有没有办法从 Google Apps 脚本中访问 JavaScript 全局窗口变量?

小智 5

Apps 脚本无法使电子表格显示在客户端浏览器中,因为它是服务器端脚本。

该方法openById返回电子表格上的句柄,从而可以通过 Apps 脚本操作电子表格。这就是“打开电子表格”对于脚本的含义。此操作对用户界面没有影响。

这个方法setActiveSpreadsheet几乎没什么用;它仅更改脚本调用时返回的电子表格getActiveSpreadsheet

一种对 UI 确实有影响的方法是setActiveSheet:如果将其应用于用户在浏览器中打开的电子表格,则该方法可以更改该电子表格的哪个选项卡/工作表面向用户。