Search code examples
google-apps-scriptgoogle-docsurlfetchgoogle-data-api

Script running fine in a sheet, not in another


I have two sheets, both of them in a shared drive. Both have the very same script to send html emails based upon a Google Doc (also located in the shared drive) : first I get the html code, and then send the mail.

Everything runs fine in a sheet, but I get a 404 if I run it in the other sheet.

I first thought of a permissions issue, but I ran it from two different accounts, and I have the same behavior. The odd thing is that this script used to run absolutely fine...

Here's the script :

function docToHtml(templateId){
  var url = "https://docs.google.com/feeds/download/documents/export/Export?id="+templateId+"&exportFormat=html";
  var param = 
      {
        method      : "get",
        headers     : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
        muteHttpExceptions:false,
      };
  var html = UrlFetchApp.fetch(url,param).getContentText();
  return html;
}

Well, as it runs fine in one of the sheets, the script must not be the culprit. I've tried to figure this out, but I am out of thoughts.


Solution

  • Error 404 means "Not found" so it's possible that the spreadsheet id is wrong or the OAuth credentials that you are using hasn't permission to access the corresponding files.

    If you are calling the function from dialog or sidebar maybe the wrong credentials are being passed to the server side script.

    Another possibility is that the script scopes doesn't include one of those that allow to access a file hosted in Google Drive1.

    NOTES:

    1. This possibility was confirmed by the OP through a comment to this answer.

    If you are open to use an alternative, I suggest you to use `DriveApp` or `Drive` instead of Google Documents (DATA) API as nowadays there aren't official reference doc about
    GET /feeds/download 
    

    Anyway there are a lot of questions using similar forms i.e.

    https://docs.google.com/feeds/download/spreadsheet/export/Export?=...
    https://spreadsheet.google.com/feeds/download/spreadsheet/export/Export?=...
    

    Related