Use DriveApp.createFile() in Add-on

223 views
Skip to first unread message

Allard Rothengatter

unread,
Apr 14, 2020, 9:00:54 AM4/14/20
to google-apps-sc...@googlegroups.com
Hey guys,

I am writing an add-on for use within our organisation only. Going pretty well so far, but now I'd like to use DriveApp.createFile() to create new spreadsheets and PDF's in a (shared) folder. I have to use the DriveApp scope for moving files later on in this project as well.

When trying to use DriveApp.createFile() I keep getting the following error:

Exception: We're sorry, a server error occurred. Please wait a bit and try again

I think this has something to do with the authorization of the Add-on. As you guys can see in the attachment, I've added a few scopes to the Marketplace SDK. 

Been looking all over Google to find a solution but haven't found it yet. Can you guys point me in the right direction?

Thanks much...

Cheers,
Allard Rothengatter
Screenshot 2020-04-14 at 14.56.12.png

Dimu Designs

unread,
Apr 14, 2020, 9:25:48 AM4/14/20
to Google Apps Script Community
That error message has always been very ambiguous. Without seeing the actual code its difficult to determine whether or not your call to  DriveApp.createFile() is the cause. If possible, please post the script for review. 

Allard Rothengatter

unread,
Apr 14, 2020, 9:33:30 AM4/14/20
to Google Apps Script Community
Thanks for your quick reply. 

Here's my code (where getProjectSpecs() just gets some values out of the active spreadsheet for usage):

function generatePdf(){
  SpreadsheetApp.flush();

  var projectSpecs = getProjectSpecs();
  
  //make pdf
  + projectSpecs.docId
  + '/export?exportFormat=pdf&format=pdf'
  + '&size=A4'
  + '&portrait=true'
  + '&fitw=true'       
  + '&top_margin=0.75'              
  + '&bottom_margin=0.75'          
  + '&left_margin=0.70'             
  + '&right_margin=0.70'           
  + '&sheetnames=false&printtitle=false'
  + '&pagenum=false'
  + '&gridlines=false'
  + '&fzr=FALSE'      
  + '&gid='
  + projectSpecs.sheetId;       //the sheet's Id
  
  var token = ScriptApp.getOAuthToken();
  var docUrl = UrlFetchApp.fetch(theUrl, { headers: { 'Authorization': 'Bearer ' +  token } });
  var fileId = DriveApp.createFile(docUrl.getBlob()).setName(projectSpecs.pdfTitle).getId();

  Browser.msgBox('File Created')

  var pdf = docUrl.getBlob().setName(projectSpecs.pdfTitle);
}

It will not display the msgBox with the 'File Created' message.

Vinicius Mello

unread,
Apr 14, 2020, 11:51:06 AM4/14/20
to google-apps-sc...@googlegroups.com
I believe that you will need to enable Drive API in your project. I got some issues and have solved it like this.

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/8356391b-e2bb-4b1d-9a70-ba66e882e63e%40googlegroups.com.

Allard Rothengatter

unread,
Apr 14, 2020, 12:03:50 PM4/14/20
to Google Apps Script Community
Yeah, this was it. Sorry guys for troubling you. This was so easy, could have figured this out myself...

Thanks much everyone.



On Tuesday, 14 April 2020 15:00:54 UTC+2, Allard Rothengatter wrote:
Reply all
Reply to author
Forward
0 new messages