Search code examples
google-apps-scriptgoogle-slides-apiurlfetch

appscript UrlFetchApp and slides export to txt


I sincerely someone can help me with this. My end goal is to be able to get the plain text representation of a google slides deck just like what you get when you are in the editor and go to File->Download-Plain text. I need to get this txt inside my google appscript code as a string.

From what I have gathered so far this is not possible using the Slides API. Objects like Presentation, Slide etc don't appear to have export asString() methods. So I looked at the http requests executed when I use the UI in editor mode.

Turns out that if you replace the /edit in your URL with /export/txt of any google slide deck you get redirected to an export to txt.

So I coded this inside my appscript project with URL fetch to get to it:

var l_sDocID = _deck.getId();                            // A Drive File
var l_httpOptions = {"contentType" : "text/plain",
                     "method" : "get",
                     "followRedirects" : true,
                     "muteHttpExceptions" : true,
                     "Authorization" : "Bearer " + ScriptApp.getOAuthToken()};
var l_sExportURL = "https://docs.google.com/presentation/d/" + l_sDocID + "/export/txt";
logger.log(UrlFetchApp.fetch(l_sExportURL, l_httpOptions).getContentText());

For the life of me I cannot figure out what I am doing wrong but I get Error 400 each time. When I hit the same URL in my browser which is already authenticated to the google domain the redirect happens and I get the content I am after.

If there are other ways to do this instead of http round trip inside google I am happy to look at that but I haven't found one yet.

My project is based on the REST wrappers so uses DriveApp etc. I don't know if one can mix this with the V2 REST api without wrappers but if so I am happy to consider that also if it will address this issue.

Please help anyone?


Solution

  • I believe your goal as follows.

    • You want to export a Google Slides as a text data using UrlFetchApp with Google Apps Script.

    For this, how about this answer?

    Modification points:

    • About the endpoint for exporting the text data, when you want to use the Files: export method in Drive API, please use "https://www.googleapis.com/drive/v3/files/" + l_sDocID + "/export?mimeType=text/plain" as l_sExportURL.
      • Also, you can use "https://docs.google.com/feeds/download/presentations/Export?id=" + l_sDocID + "&exportFormat=txt" as the endpoint.
    • In your request header, headers is not included. Please include Authorization in headers.
    • followRedirects is true as the default.
    • method is get as the default.
    • In the get method, contentType is not required.
    • logger.log is not correct. It's Logger.log.

    When above points are reflected to your script, it becomes as follows.

    Modified script:

    var l_sDocID = _deck.getId();  // This is the Google Slides ID.
    var l_httpOptions = {
      muteHttpExceptions : true,
      headers: {Authorization : "Bearer " + ScriptApp.getOAuthToken()}
    };
    var l_sExportURL = "https://www.googleapis.com/drive/v3/files/" + l_sDocID + "/export?mimeType=text/plain";
    var res = UrlFetchApp.fetch(l_sExportURL, l_httpOptions).getContentText();
    Logger.log(res);
    

    Note:

    References: