0

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

saurav
  • 5,388
  • 10
  • 56
  • 101
  • You don't show us all of the relevant code hence I don't know if the mistake I see here is related: you have `if(host = 'sheets')` there, which sets your `host` to be equal `'sheets'`, instead of checking if it already equals `'sheets'` – grreeenn Mar 15 '21 at 16:51
  • sorry..that was a typo...i have corrected it now....and added some more details of how i am forming the request – saurav Mar 15 '21 at 17:21

1 Answers1

1

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

Ron M
  • 5,791
  • 1
  • 4
  • 16
  • Thanks Ron for the reply...so we need to call Drive APIs and get the blob to app script runtime..problem with this approach i have to fetch the content from Drive to app script runtime and then send to my backend service...is there something available in app script runtime itself ? – saurav Mar 18 '21 at 07:06
  • Not sure what do you mean by apps script runtime?, We just used the Advanced Drive Service in apps script to get the MS office export link of the file and use UrlFetchApp to get the file blob based on the export link. This complete procedure was done in apps script. – Ron M Mar 18 '21 at 15:55
  • what i meant is there any way to directly get the blob through AppScript APIs like DocumentApp rather than going through Drive APi and get it ?...would fetching and sending the content as you proposed be fine performance wise than just getting the blob through DocumentApp APIs ? – saurav Mar 18 '21 at 19:19
  • I tried using SpreadsheetApp, DocumentApp and DriveApp to get the blob and use Blob.getAs(contentType) to convert the pdf to ms office format but conversion is not supported. The only option I have in mind is to manually create an export format link to eliminate using advanced drive service. But we still need to use UrlFetchApp to get the blob based on the export format link. See the updated answer. – Ron M Mar 18 '21 at 20:45
  • Regarding your question for performance, I really can't tell whether there will be some impacts in your program. But as far as my research goes, using urlfetchapp is the most commonly used when converting files to other formats. – Ron M Mar 18 '21 at 20:46
  • Thanks Ron your answers have helped a lot – saurav Mar 21 '21 at 15:31