google-sheetsgoogle-apps-scriptgoogle-slides

trying to use Google Appscript to create a slide presentation from the data in a specific sheet. error TypeError: ss.getSheetById is not a function


Here is my code:and I keep getting the error message "TypeError: ss.getSheetById is not a function

I originally had a try catch block but when I had that in, the error messages kept popping up as error: _______. There was nothing after the colon, after getting rid of the try catch block, now I get the error message stated earlier.

  const spreadsheetId = '1z1HAUVNnoAtMUa4DtlcbJ21EPBHfHrAFEF4a9XsRgMA';
  const sheetID = 0;

  // Ensure correct import
  SpreadsheetApp;

  const ss = SpreadsheetApp.openById(spreadsheetId);

  if (!ss) {
    Logger.log("Error: Could not open spreadsheet.");
    return;
  }

  const sheet = ss.getSheetById(sheetID);

  // Access data from the sheet (example: get the value in cell A1)
  const cellValue = sheet.getRange("A1").getValue();
  Logger.log("Value in cell A1:", cellValue);

  // Create a new presentation based on data from the sheet
  const presentation = SlidesApp.create("Test Presentation");
  const slide = presentation.appendSlide(SlidesApp.PredefinedLayout.TITLE_AND_BODY);

  // Add content to the slide based on the sheet data
  const title = slide.getPageElements().find(element => element.getType() === SlidesApp.PageElementType.SHAPE);
  title.insertText(0, "Data from Sheet A1: " + cellValue);

  Logger.log("Presentation created successfully!");
}



Solution

  • getSheetById Feature Not Yet Implemented

    Based from the Class Spreadsheet article, getSheetById does not exist.

    On the other hand, there is an Issue Tracker that was posted on 2014, getSheetById feature is not yet implemented. The request's status is still assigned.

    For the meantime, you may use getSheetByName. You may use this function as follows:

    SpreadsheetApp.openById("Spreadsheet_ID").getSheetByName("Sheet_Name");
    

    References: