简体   繁体   English

如何以编程方式从谷歌脚本打开一个新的电子表格

[英]how to programmatically open a new spread sheet from google scripts

in setActiveSpreadSheet doc the example is very clear:setActiveSpreadSheet文档中,示例非常清楚:

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

however implementing that inside my google scripts, nothing happens at all (not even an error message).. ideas?但是在我的谷歌脚本中实现它,根本没有任何反应(甚至没有错误消息)......想法?

update更新

Based on the answers below, it's clear that the above code has no effect on UI.根据下面的答案,很明显上面的代码对 UI 没有影响。 I tried doing this:我尝试这样做:

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

but then I got this error:但后来我收到了这个错误:

ReferenceError: "window" is not defined.

Is there a way to access the javascript global window variable from within google scripts?有没有办法从谷歌脚本中访问 javascript 全局窗口变量?

An Apps Script cannot make a spreadsheet appear in a client's browser, since it is a server side script. Apps 脚本无法使电子表格出现在客户端的浏览器中,因为它是服务器端脚本。

The method openById returns a handle on a spreadsheet which makes it possible to manipulate with the spreadsheet from the Apps Script.方法openById返回电子表格的句柄,这使得可以从 Apps 脚本使用电子表格进行操作。 This is what "to open a spreadsheet" means for a script.这就是“打开电子表格”对于脚本的意义。 This action has no effect on user's interface.此操作对用户界面没有影响。

The method setActiveSpreadsheet is pretty much useless;方法setActiveSpreadsheet几乎没用; it only changes which spreadsheet will be returned when a script calls getActiveSpreadsheet .它只会更改脚本调用getActiveSpreadsheet时将返回的电子表格。

One method that does have effect on UI is setActiveSheet : if it is applied to a spreadsheet that a user has opened in their browser, the method can change which tab/sheet of that spreadsheet is facing the user.对 UI 有影响的一种方法是setActiveSheet :如果将其应用于用户在浏览器中打开的电子表格,则该方法可以更改该电子表格的哪个选项卡/工作表面向用户。

I guess, you can't just open new tab in browser and open new spreadsheet by script.我想,您不能只是在浏览器中打开新标签页并通过脚本打开新电子表格。 That's because script can't control browsers.那是因为脚本无法控制浏览器。 But you could get data from closed Spreadsheet:但是您可以从关闭的电子表格中获取数据:

function openSheetTest() {
  var newWs = SpreadsheetApp.openById('1234567890');
  var sheet = newWs.getSheetByName('Sheet1');

  var data = sheet.getDataRange().getValues();

  Logger.log(data);
}

There's is a class Browser in G-sheets. G-sheets 中有一个类浏览器 And all we can for now is to make promts or MsgBoxes.我们现在所能做的就是制作提示或 MsgBoxes。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从谷歌电子表格动态设置值到另一个谷歌电子表格单元格 - How to set dynamically value from a google spread sheet to another google spread sheet cell 将数据从数组发布到Google电子表格 - Publishing data from an array to a google spread sheet 如何使用 Google App Scripts 从工作表名称创建字符串? - How to create a string from the sheet names using Google App Scripts? 如何从 Google App Scripts 中的另一个工作表中提取数据验证 - How to pull data validation from another sheet in Google App Scripts 如何使用谷歌脚本检查两个谷歌工作表,并将重复项移动到新工作表? - How to use google scripts to check two google sheets, and move the duplicates to a new sheet? 使用Google电子表格中的经纬度长点的 - heat map using lat long points from google spread sheet 从HTML表单向Google电子表格发送数据 - Sending data to google spread sheet from a HTML form 如何使用谷歌脚本自动将表单响应从谷歌表格移动到谷歌文档? - How can I automate moving form responses from a google sheet to a google doc using google scripts? Google脚本-如何调用正确的表格 - Google Scripts - How to call the correct sheet Google表格脚本onOpen(e)向表格添加新行 - Google Sheets Scripts onOpen(e) Add New Row to Sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM