SpreadsheetApp.getActiveSpreadsheet() is breaking

2019-06-09 16:37发布

I'm writing my 1st google app script. Spreadsheet opens a sidebar in onOpen(). Sidebar has Button and in SidebarJavaScript.html I have listener to that button. I'm calling SpreadsheetApp.getActiveSpreadsheet() inside listener and after this line script is not executing.

What could possibly be wrong here?

function onScrapeClick(){

    // Disable Button
    this.disabled = true;

    // Get Spreadsheet
    var sheet = SpreadsheetApp.getActiveSpreadsheet();
    this.innerHTML ="Clicked";
    var data = sheet.getDataRange().getValues();

}

Button is getting disabled as for line this.disabled = true; as expected so lister is working properly for sure.

2条回答
Fickle 薄情
2楼-- · 2019-06-09 17:16

getValues is a method of the sheet class, you just need to define the active sheet

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
查看更多
Luminary・发光体
3楼-- · 2019-06-09 17:28

You must define the method to get data in a .gs file and call it with google.script.run.

In some Code.gs file:

function getSheetData()
{
   var sheet = SpreadsheetApp.getActiveSpreadsheet();
   var data = sheet.getDataRange().getValues();
   return data;       
}

And in the html script:

function onScrapeClick(){

  // Disable Button
  this.disabled = true;

  // Get Spreadsheet

  this.innerHTML ="Clicked";
  google.script.run
    .withSuccessHandler(
      function(data, element) {
        // code to execute if data was gotten ok
        // the received data is in data argument
      })
    .withFailureHandler(
      function(msg, element) {
        // code to execute if data was not gotten ok
      })
    .getSheetData();

}
查看更多
登录 后发表回答