Search code examples
google-apps-scriptgoogle-sheets-apispreadsheetdocument

Error Exception: Service Spreadsheet failed while accessing document with id


I could not find the solution to this error in the previous answers. I'm running this script under Google Apps Scripts with Google Sheets. So I would like you to review my simple line of code to see if there is something I overlooked:

var firstCSV = SpreadsheetApp.openByUrl("https://drive.google.com/file/d/14EA51XsRpohpFVCwpO3sZwf_H3o6Dp_o/edit#gid=0");

Error on this line -

Exception: Service Spreadsheets failed while accessing document with id 14EA51XsRpohpFVCwpO3sZwf_H3o6Dp_o. GregReport @ Receipts Calculator.gs:37

  • I first tried using "openById" using just the ID number and got the same error message.

  • Next I tried using the "openByUrl" using the document's URL and got
    the same error message.

  • I checked the file on Google drive and it's there and I can open it.

  • I ran this line of code in a script by itself and got the same error.

  • I changed the URL in the code to a Sheets document URL an I got the
    same error.

  • The original file has a .csv extension but my understanding is this
    should work just fine in Sheets.

  • With both document formats and different methods I'm getting this
    same error.

  • Any suggestions, or code updates I'm not aware would be appreciated. All I'm trying to do is open a spreadsheet and extract some data from it using a script built in Sheets.


Solution

  • Unfortunately, it is not possible to directly call a CSV file as a spreadsheet in Apps Script whether by using the file ID or the drive url.

    What you need to do is to parse the CSV and get the content of it and paste it into a spreadsheet in Apps Script. Please see sample code below:

    function myFunction() {
      var file = DriveApp.getFilesByName(your-filename-here).next();
      var csvData = Utilities.parseCsv(file.getBlob().getDataAsString());
      var sheet = SpreadsheetApp.getActiveSheet();
      sheet.getRange(1,1,csvData.length, csvData[0].length).setValues(csvData)
    }