1)Is there a Google Appscript command that can con

2019-08-26 19:39发布

问题:

1)Is there a Google appscript command that can convert a .gDraw to a .jpg?
2)Can a Google trigger be set to perform this conversion on a .gdraw file edit?

1) My answer so far (in google/pseudo code)

function gDrawTO_jpeg(){
var gDrawFile = DriveApp.getFilesByID('gDraw_fileID')
gDrawJpg = gDrawFile.getAs("image/jpg") // doesn't work due to current getAs() limits.
gDrawPdf = gDrawFile.getAs("application/pdf");} //doesn't work due to known glitch, link below

Glitch in converting file to pdf on Google: https://code.google.com/p/google-apps-script-issues/issues/detail?id=3579.

Is there possibly an obscure blob function workaround for this?

2) My answer so far:

function triggerSet(){
ScriptApp.newTrigger('gDrawTO_jpeg')
   .forDocument('gDraw_fileID')
   .onOpen()
   .create();//returns error on this line}

Only works on google docs/sheets/forms, no success with gDraw's. Is there a blob work around?

**As a post-remark to this question, it would be great for gDrive/gDocs to have a feature or option to auto-save docs/sheets/gDraws in standard or non-Google formats (.doc/.xls/.jpg respectively, they have this feature manually but there are obvious various advantages to this being automated).

回答1:

Use the work-around detailed in comment #22 in the apps script issue 3579 you liked to.

Important: you must enable Drive API in Advanced Services for your project and in Developers Console for the work-around to work:

  1. In your script project go to menu Resources->Advanced Google services and turn Drive API service on.
  2. Click on Developers Console link at the bottom of the Advanced Google Services dialog box and also enable Drive API there.

After you have done the above the following code will get your Drawing as JPEG file:

function gDrawTO_jpeg(){
  var gDrawFile = Drive.Files.get('your_draw_file_ID_here'); 
  var url = gDrawFile.exportLinks['image/jpeg'];
  var token = ScriptApp.getOAuthToken();
  var response = UrlFetchApp.fetch(url, {
    headers: {
      'Authorization': 'Bearer ' +  token
    }
  });
  var jpeg = response.getBlob();
  // save drawing as jpeg file to Drive, or do whatever you need to with the blob
  DriveApp.createFile(jpeg);
}

As for the second part of your question: no. Google Draw does not support apps script (yet).