Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReferenceError: "Sheets" is not defined

This is my first attempt with script editor. I was assigned to do a script to crate pivot table for google sheet.

  //creating pivot table through script editor for google sheet
  function addPivotTable() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheetName = "Sheet1";

  // Create a new sheet which will contain our Pivot Table
  var pivotTableSheet = ss.insertSheet();
  var pivotTableSheetId = pivotTableSheet.getSheetId();

 // Add Pivot Table to new sheet
 // Meaning we send an 'updateCells' request to the Sheets API
 // Specifying via 'start' the sheet where we want to place our Pivot Table
 // And in 'rows' the parameters of our Pivot Table 

  var requests = [{
    // Meaning we send an 'updateCells' request to the Sheets API
    "updateCells": {
        // And in 'rows' the parameters of our Pivot Table 
       "rows": {
         "values": [
      {
         // Add Pivot Table to new sheet
        "pivotTable": {
          "source": {
            "sheetId": ss.getSheetByName(sheetName).getSheetId(),
            "startRowIndex": 0,
            "startColumnIndex": 0,
          },
          //create rows from the selected columns
          "rows": [
            {
              "sourceColumnOffset": 14,
              "showTotals": true,
              "sortOrder": "ASCENDING",   
            },
          ],
          //show values from the selected columns
          "values": [
            {
              "summarizeFunction": "COUNTA",
              "sourceColumnOffset": 10
            }
          ],
          //display in horizontal layout
          "valueLayout": "HORIZONTAL"
        }
      }
    ]
  },
  // Specifying via 'start' the sheet where we want to place our Pivot Table
  "start": {
    "sheetId": pivotTableSheetId,
  },
  "fields": "pivotTable"
}
 }];

  Sheets.Spreadsheets.batchUpdate({'requests': [requests]}, ss.getId());
}

Please do check my code and explain where did i went wrong as every time im running the script editor error telling sheet is not defined popup. "ReferenceError: "Sheets" is not defined. (line 46, file "Code")Dismiss"

like image 535
Mokan Velan Avatar asked Aug 11 '17 02:08

Mokan Velan


Video Answer


1 Answers

Just add the required service from here, in your case you need Google Sheets API. Good luck.

Just add the service from here, Good Luck

like image 177
Shibi G.krishnan Avatar answered Sep 17 '22 16:09

Shibi G.krishnan