1

So I am trying to write a Google script that will create an image file from 3 different image files that are on google drive. The method that I have decided to use was to create a template or a base gDraw file in Drive and then modify the file and export it as a jpeg. So far I've been able to export my gDraw file as image using the code from this post 1)Is there a Google Appscript command that can convert a .gDraw to a .jpg? 2)Can a trigger be set to perform this conversion on .gdraw file edit? .

function gDrawTO_jpeg(){
  var gDrawFile = Drive.Files.get('1P2qYTyeI9RovsI_qwiMEtlBeTJw-exjckQeYLL_NA7w'); 
  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);
}

What I'm stuck on is trying to figure out how to replace the images in the gDraw file.

Thanks in advance for any help!

Community
  • 1
  • 1
amullins65
  • 11
  • 3

2 Answers2

0

I've never come across API for Google Draw, which mean is wouldn't contactable by script for editing. However, just a month ago Google came out with the Slide API which you can remotely add images to. Don't know if this helps, the slide could then be embedded.

Jason Allshorn
  • 1,625
  • 1
  • 18
  • 27
0

Thanks for the direction. Here is where I've gotten by adapting some different code I have found. I currently have a slide with one image that never changes and 2 shapes that represent where the other 2 images go. I build a new copy of the slide and then call downloadPresentation(id)from this post How to download Google Slides as images? to export the new slide as a PNG!

function slides_template() {


  var IMG_FILE = 'IMG_4096.JPG'      
  var TMPLFILE = 'Grid Template' 
  var SZIMG_FILE = 'onesize.jpeg'  


  SLIDES.setTokenService(function(){returnScriptApp.getOAuthToken()});


  Logger.log('** Copying template **');
  var DECK_ID = DriveApp.getFilesByName(TMPLFILE).next().makeCopy().getId();


  Logger.log('** Searching for files');
  var rsp = DriveApp.getFilesByName(IMG_FILE).next();
  var szrsp = DriveApp.getFilesByName(SZIMG_FILE).next();

  Logger.log(Utilities.formatString(' - Found image %s', rsp.getId()));

  var img_url = rsp.getDownloadUrl()+"&access_token="+ScriptApp.getOAuthToken();
  var szimg_url = szrsp.getDownloadUrl()+"&access_token="+ScriptApp.getOAuthToken();


  Logger.log('Inserting Images');
var reqs = [    

  {"replaceAllShapesWithImage": {

  "imageUrl": img_url,
  "replaceMethod": "CENTER_CROP",
  "containsText": {
           "text": "Main_Image",
           "matchCase": "True",
               }
  },},

    {"replaceAllShapesWithImage": {

  "imageUrl": szimg_url,
  "replaceMethod": "CENTER_CROP",
  "containsText": {
           "text": "Size_Image",
           "matchCase": "True",
               }
  },},

  ];


  SLIDES.presentationsBatchUpdate(DECK_ID, {'requests': reqs});

  downloadPresentation(DECK_ID)

  Logger.log('DONE'); 
}
Community
  • 1
  • 1
amullins65
  • 11
  • 3