最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cannot find method setActiveSheet(string) - Stack Overflow

programmeradmin1浏览0评论

In Google Sheets, which runs on JavaScript, I'm getting this message, "Cannot find method setActiveSheet(string). (line 4, file "Code")", I don't know why... I'm pretty new to coding, so bear with me.

function onOpen() {
  var email = Session.getActiveUser().getEmail();
  var sheet = email.slice(0,-11);
  SpreadsheetApp.setActiveSheet(sheet)
}

In Google Sheets, which runs on JavaScript, I'm getting this message, "Cannot find method setActiveSheet(string). (line 4, file "Code")", I don't know why... I'm pretty new to coding, so bear with me.

function onOpen() {
  var email = Session.getActiveUser().getEmail();
  var sheet = email.slice(0,-11);
  SpreadsheetApp.setActiveSheet(sheet)
}
Share Improve this question asked Feb 1, 2015 at 2:17 NonCreature0714NonCreature0714 6,03410 gold badges33 silver badges55 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

You're getting the "Cannot find method" error because the setActiveSheet() method takes an argument of type Sheet, not a string. See the specification here: https://developers.google./apps-script/reference/spreadsheet/spreadsheet-app#setActiveSheet(Sheet)

In order to get a Sheet object from the string, you need to open the parent Spreadsheet, then get the appropriate sheet by name, then you can pass it to setActiveSheet.

Assuming this script is embedded in the relevant Spreadsheet, that looks like this:

var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
SpreadsheetApp.setActiveSheet(spreadsheet.getSheetByName(sheet));

This worked well for me.

function theSecondSheet() {
   var activeSpreadSheet = SpreadsheetApp.getActiveSpreadsheet();
    if(activeSpreadSheet.getSheetByName("Sheet2").activate()){
     SpreadsheetApp.setActiveSheet(activeSpreadSheet.getSheetByName("Sheet2"));
    }
}
发布评论

评论列表(0)

  1. 暂无评论