Labelling Slides that are converted to PNG based off a column in Google Sheet

80 views
Skip to first unread message

Karina Ng

unread,
Sep 30, 2021, 3:15:55 PM9/30/21
to Google Apps Script Community
Does anyone know how to label slides according to a column in Google Sheets when converted to a PNG. i.e. Slide 1 is labelled A1 from google sheets, Slide 2 is labelled A2 from google sheets, Slide 3 is A3 etc. 

What's occurring is now is each slide is returned with the same row (Slide 1 = A1, Slide 1 = A2, Slide 1 = A3), and then will return the each slide of the presentation again but with the next row of data (Slide 2 = A1, Slide 2 = A2, Slide 2 = A3)....

var dataSpreadsheetUrl = "xxx";
var ss = SpreadsheetApp.openByUrl(dataSpreadsheetUrl);
var sheet = ss.getSheetByName('Connected sheet 1');
var values = sheet.getRange('A1:A14').getValues();

// Log URL of the main thumbnail of the deck
Logger.log(Drive.Files.get(presentationId).thumbnailLink);

// For storing the screenshot image URLs
var screenshots = [];

var slides = presentation.getSlides().forEach(function(slide, index) {
values.forEach(function(page){
if(page[0]){
var title_name = page[0];
var url = baseUrl
.replace("{presentationId}", presentationId)
.replace("{pageObjectId}", slide.getObjectId());
var response = JSON.parse(UrlFetchApp.fetch(url, parameters));
// Upload Googel Slide image to Google Drive
var blob = UrlFetchApp.fetch(response.contentUrl).getBlob();
DriveApp.createFile(blob).setName("Image " + (title_name) + ".png");
screenshots.push(response.contentUrl);
}});
});  

This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this email in error please notify the system manager. This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited.

Tanaike

unread,
Sep 30, 2021, 9:29:59 PM9/30/21
to Google Apps Script Community
I cannot understand about "label" you expect. I apologize for my poor English skill. Can I ask you about the detail of your goal?

Karina Ng

unread,
Oct 5, 2021, 6:17:31 AM10/5/21
to Google Apps Script Community
Sorry for my late response. 

I have powerpoint with x number slides for example, slide 1, slide 2, slide 3, slide 4, slide 5. I want to convert this into a PNG file and name the file according to the information in a google sheet. 
Google Sheets: Column A, 
A1: DA-1501 
A2: DA-1502 
A3: DA-7205 
A4: DA-7203
A5: DA-2342
.....

GOAL:
Slide 1 = DA-1501.png
Slide 2 = DA-1502.png
Slide 3 = DA-7205.png
....... etc.

Hope this makes sense and thank you for your time. 

Tanaike

unread,
Oct 5, 2021, 10:02:25 PM10/5/21
to Google Apps Script Community
Thank you for replying. In your script, I thought of the following modification points.

- "presentation" is not declared.
- "baseUrl" is not declared.
- The value of `var values = sheet.getRange('A1:A14').getValues();` is 2 dimensional array.

When these points are reflected in the script, it becomes as follows.


function myFunction() {
  var presentationId = "###"; // Please set Google Slides ID.
  var spreadsheetId = "###"; // Please set Spreasdheet ID.

  var ss = SpreadsheetApp.openById(spreadsheetId);
  var sheet = ss.getSheetByName('Connected sheet 1');
  var values = sheet.getRange('A1:A5').getValues();
  var slides = SlidesApp.openById(presentationId).getSlides();
  if (values.length > slides.length) throw new Error("Check number of titles and pages.");
  var screenshots = values.map(([title_name], i) => {
    var slide = slides[i];
    var response = UrlFetchApp.fetch(url, {headers: {authorization: "Bearer " + ScriptApp.getOAuthToken()}});
    var obj = JSON.parse(response.getContentText());
    var blob = UrlFetchApp.fetch(obj.contentUrl).getBlob();
    DriveApp.createFile(blob).setName(title_name + ".png");
    return obj.contentUrl;
  });
  console.log(screenshots);
}

Reply all
Reply to author
Forward
0 new messages