javascript - How to programmatically open a new spreadsheet - Stack Overflow

In setActiveSpreadSheet doc the example is very clear: The code below will make the spreadsheet with

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('/'+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 Apps Scripts?

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

Share Improve this question edited Oct 2, 2022 at 18:46 Wicket 38.8k9 gold badges80 silver badges195 bronze badges asked Jun 3, 2016 at 13:41 abboodabbood 23.6k20 gold badges141 silver badges258 bronze badges 1
  • 2 Once again: Apps Script is server side. It knows no such thing as "window" or "document". – user3717023 Commented Jun 4, 2016 at 4:57
Add a ment  | 

2 Answers 2

Reset to default 5

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.

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. And all we can for now is to make promts or MsgBoxes.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744888870a4599282.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信