Search code examples
google-apps-scriptgoogle-workspace

documentapp.getactivedocument().getBlob gives pdf


i am trying to send the google document's content to my backend service.

In the app script i am using

if(host == 'sheets'){
  var content = SpreadsheetApp.getActiveSpreadsheet().getBlob();
}else if (host == 'docs') {
  var content = DocumentApp.getActiveDocument().getBlob();
}

I take the blob and sent it through multi part form request in URLFetchApp.fetch() through the payload parameter.

But the content for both the docs and sheets is converted/sent to my service as pdf.

Is there any way to preserve/send the files in google format itself ?

if not in google format then in Microsoft office formats ?

Best Regards,

Saurav


Solution

  • As mentioned in the similar post, this behavior is expected. If you want to get the file's content in Microsoft office formats, you can check the following options:

    (OPTION 1): Get export URL from Advanced Drive Service

    Sample Code:

    function getDocument(){
      
      var host = "docs";
      var fileId;
      var exportFormat;
    
      if(host == 'sheets'){
        fileId = SpreadsheetApp.getActiveSpreadsheet().getId();
        exportFormat = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
      }else if (host == 'docs') {
        fileId = DocumentApp.getActiveDocument().getId();
        exportFormat = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
      }
    
      var url = Drive.Files.get(fileId).exportLinks[exportFormat];
      Logger.log(url);
      var oauthToken = ScriptApp.getOAuthToken();
      var content = UrlFetchApp.fetch(url, {
        headers: {
          'Authorization': 'Bearer ' + oauthToken
        }
      }).getBlob();
      Logger.log(content.getContentType());
      content.setName("TestFile");
      MailApp.sendEmail("email@sample.com", "Test", "Test", {attachments: content});
    }
    

    Pre-requisite:

    You need to enable Advanced Drive Service to get the export links of the file using Drive.Files.get(). This request will return a File Resource where you can get the exportLinks that can be accessed using a key based on the supported export MIME Types

    enter image description here

    enter image description here


    What it does?

    1. Depending on the host set, get the file id and export format based on supported export MIME Types
    2. Get the file resource using Drive.Files.get() and get the export link based on the export format key set in step 1.
    3. Use UrlFetchApp.fetch(url, params) and get the file's blob using HTTPResponse.getBlob()

    Output:

    enter image description here


    (OPTION 2):Create export URL manually using the template url

    Sample Code:

    function getDocument(){
      
      var host = "docs";
      var fileId;
      var url;
    
      if(host == 'sheets'){
        fileId = SpreadsheetApp.getActiveSpreadsheet().getId();
        url = "https://docs.google.com/spreadsheets/export?id="+fileId+"&exportFormat=xlsx"
      }else if (host == 'docs') {
        fileId = DocumentApp.getActiveDocument().getId();
        url = "https://docs.google.com/feeds/download/documents/export/Export?id="+fileId+"&exportFormat=docx";
      }
    
      Logger.log(url);
      var oauthToken = ScriptApp.getOAuthToken();
      var content = UrlFetchApp.fetch(url, {
        headers: {
          'Authorization': 'Bearer ' + oauthToken
        }
      }).getBlob();
      Logger.log(content.getContentType());
    }
    

    What it does?

    1. Depending on the host set, get the file id and create an export link using this templates:

    EXCEL: https://docs.google.com/spreadsheets/export?id=<fileId>&exportFormat=xlsx

    WORD: https://docs.google.com/feeds/download/documents/export/Export?id=<fileId>&exportFormat=docx

    1. Use UrlFetchApp.fetch(url, params) and get the file's blob using HTTPResponse.getBlob()

    Note:

    Based on Quotas for Google Services, Url Fetch Call has a daily quota of 20,000 for Consumer and G Suite free edition, while 100,000 for Google Workspace accounts