如何以编程方式从Google脚本中打开新的电子表格 [英] how to programmatically open a new spread sheet from google scripts

本文介绍了如何以编程方式从Google脚本中打开新的电子表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

setActiveSpreadSheet doc中的示例非常清晰:

  //下面的代码将使电子表格中的键1234567890成为活动电子表格
var ss = SpreadsheetApp .openById( 1234567890);
SpreadsheetApp.setActiveSpreadsheet(ss);

然而,在我的谷歌脚本中实现它,根本没有任何反应(甚至没有发生错误信息)..想法吗?
$ b $ h1 update

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

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

但是我得到这个错误:

  ReferenceError:窗口未定义。 

有没有一种方法可以从Google脚本中访问javascript全局窗口变量?

解决方案

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

方法 openById 会在电子表格中返回一个句柄,使得可以使用Apps脚本中的电子表格进行操作。这就是打开电子表格对于脚本的意义。这个动作对用户界面没有任何影响。



方法 setActiveSpreadsheet 几乎没用。它只会改变当脚本调用 getActiveSpreadsheet 时返回哪个电子表格。



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

in setActiveSpreadSheet doc the example is very clear:

// 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. 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?

解决方案

An Apps Script cannot make a spreadsheet appear in a client's browser, since it is a server side script.

The method openById returns a handle on a spreadsheet which makes it possible to manipulate with the spreadsheet from the Apps Script. 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; it only changes which spreadsheet will be returned when a script calls 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.

这篇关于如何以编程方式从Google脚本中打开新的电子表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆