Home > OS >  How do I upload image from Google Sheets to Google Slides
How do I upload image from Google Sheets to Google Slides

Time:08-12

As the title suggests, I currently have codes taking a screenshot of a website and putting it on my Google Sheets. My question is how to then transfer that image to the presentation? The image might change so it would be better if can be dynamically changed. I saw the class ReplaceAllShapesWithImageRequest but there were not a lot of examples, and they used URL instead of cell from Google Sheets. Thank you for the help!

Edit
Also there might be more images(around 9), and there are placeholders in the slides right now, but how am I able to target specific slides for specific images to fit something like this enter image description here

Where the numbers 01-08 will be the images. Thank you for the help.

Edit 2
The following is the code for snapping the screenshot

function snapScreenshot() {
  //specify where I want the image to be
  var row = 11;
  var col = 2;
  var siteUrl = sheet.getRange(row, col).getValue()
  var url = "https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url="   encodeURIComponent(siteUrl)   "&key="   pageSpeedApiKey;
  var res = UrlFetchApp.fetch(url).getContentText()
  var obj = JSON.parse(res);
  var imgData = obj.lighthouseResult.audits['final-screenshot'].details.data;
  var strImage = imgData.replace(/^data:image\/[a-z] ;base64,/, "")
  var blob = Utilities.newBlob(Utilities.base64Decode(strImage), "image/jpeg", "sample.jpeg");
  sheet.insertImage(blob, 1, 29);

   for (var i=0; i< obj.lighthouseResult.audits["screenshot-thumbnails"].details.items.length; i  ) {
    var imgData = obj.lighthouseResult.audits["screenshot-thumbnails"].details.items[i].data;
    var strImage = imgData.replace(/^data:image\/[a-z] ;base64,/, "")
    var blob = Utilities.newBlob(Utilities.base64Decode(strImage), "image/jpeg", "sample.jpeg");
    sheet.insertImage(blob, 1, 20 (i*6))
  }
}



CodePudding user response:

I kind of figure it out thanks to Tanaike's direction. Instead of getting the image from pagespeed insights api, put it on Sheets then put it on Slide, I directly put the images on the Slide instead.

function snapScreenshot(presentationId) {
  var row = 11;
  var col = 2;
  var siteUrl = sheet.getRange(row, col).getValue()
  var url = "https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url="   encodeURIComponent(siteUrl)   "&key="   pageSpeedApiKey;
  var res = UrlFetchApp.fetch(url).getContentText()
  var obj = JSON.parse(res);
  var imgData = obj.lighthouseResult.audits['final-screenshot'].details.data;
  var strImage = imgData.replace(/^data:image\/[a-z] ;base64,/, "")
  var blob = Utilities.newBlob(Utilities.base64Decode(strImage), "image/jpeg", "sample.jpeg");
  var presentation = SlidesApp.openById(presentationId)
  var page = presentation.getSlideById(slide_id)
  var position = {left:230, top: 87};
  var size = {width: 600, height: 220}
  page.insertImage(blob, position.left, position.top, size.width, size.height)
  • Related