Google Slides automation, add images from a Google Drive Folder

358 views
Skip to first unread message

John McCarthy

unread,
Mar 22, 2022, 8:08:04 AM3/22/22
to Google Apps Script Community
Can someone help, I feel I'm so close.

I feel this should be a very simple script but I'm missing something or a line of code is in the wrong place.

var presentation = SlidesApp.getActivePresentation();

function addImageSlide(images) {
var slide = presentation.appendSlide(SlidesApp.PredefinedLayout.BLANK);
var image = slide.insertImage(images);

var imgWidth = image.getWidth();
var imgHeight = image.getHeight();
var pageWidth = presentation.getPageWidth();
var pageHeight = presentation.getPageHeight();
var newX = pageWidth/2. - imgWidth/2.;
var newY = pageHeight/2. - imgHeight/2.;
image.setLeft(newX).setTop(newY);
}


function main() {

var dApp = DriveApp;
var folder = dApp.getFolderById('xxxxxxxxxxxThe drive folder Idxxxxxxxxxx');
var filesInter = folder.getFiles();
var images = [];
while(filesInter.hasNext()){
var file = filesInter.next();
var fileName = file.getName();
}
images.forEach(addImageSlide);
}


Thank you

Clark Lind

unread,
Mar 22, 2022, 2:07:54 PM3/22/22
to Google Apps Script Community
In your main function, no where are you adding the drive files to the images array. Also, since each file (image) may be a different image type, you should account for that.
Using Tanaike's script as a guide, I would do something like the below. Once you get all the image files into the array, pass the array to the next function. This is a combination of Tanaike's and your code:

function appendImages(images) {
  const s = SlidesApp.getActivePresentation();
  images.forEach((e) => {
    const image = s
      .appendSlide(SlidesApp.PredefinedLayout.BLANK)
      .insertImage(
        Utilities.newBlob(
          Utilities.base64Decode(e.data),    //this accounts for different image file types (jpg, png, etc)
          e.mimeType,
          e.fileName
        )
      );
    image.setTitle(e.fileName);
    image.setDescription(new Date().toISOString());

    var imgWidth = image.getWidth();
    var imgHeight = image.getHeight();
    var pageWidth = presentation.getPageWidth();
    var pageHeight = presentation.getPageHeight();
    var newX = pageWidth/2 - imgWidth/2;
    var newY = pageHeight/2 - imgHeight/2;
    image.setLeft(newX).setTop(newY);
  });
  s.saveAndClose();

}

function main() {

  var dApp = DriveApp;
  var folder = dApp.getFolderById('xxxxxxxxxxxThe drive folder Idxxxxxxxxxx');
  var filesInter = folder.getFiles();
  var images = [];
  while(filesInter.hasNext()){
    images.push(filesInter.next());
  }
  addImageSlide(images);
}



Clark Lind

unread,
Mar 22, 2022, 2:10:09 PM3/22/22
to Google Apps Script Community
Correction:  the last line in main should call the new function.. I forgot to change the function name to  appendImages()  insted of addImageSlide()

John McCarthy

unread,
Apr 6, 2022, 6:37:53 PM4/6/22
to Google Apps Script Community
Thanks for the help on this, I still can't get it to work.

I'm getting;

Exception: Unexpected error while getting the method or property newBlob on object Utilities.
(anonymous) @ Code.gs:7
appendImages @ Code.gs:3
main @ Code.gs:37

function appendImages(images) {
const s = SlidesApp.getActivePresentation();
images.forEach((e) => { //This is Line 3
const image = s
.appendSlide(SlidesApp.PredefinedLayout.BLANK)
.insertImage(
Utilities.newBlob(  //This Line 7
Utilities.base64Decode(e.data),        //this accounts for different image file types (jpg, png, etc)
e.mimeType,
e.fileName
)
);
image.setTitle(e.fileName);
image.setDescription(new Date().toISOString());

var imgWidth = image.getWidth();
var imgHeight = image.getHeight();
var pageWidth = presentation.getPageWidth();
var pageHeight = presentation.getPageHeight();
var newX = pageWidth/2 - imgWidth/2;
var newY = pageHeight/2 - imgHeight/2;
image.setLeft(newX).setTop(newY);
});
s.saveAndClose();

}

function main() {

var dApp = DriveApp;
var folder = dApp.getFolderById('***Folder with Images***');
var filesInter = folder.getFiles();
var images = [];
while(filesInter.hasNext()){
images.push(filesInter.next());
}
appendImages(images); //This is Line 37
}


Thanks in advance

Clark Lind

unread,
Apr 9, 2022, 10:34:23 AM4/9/22
to Google Apps Script Community
Here you go. Sorry it took so long to reply. This is working for me:

function appendImages(images) {
  var s = SlidesApp.getActivePresentation();
  images.forEach((e) => {  
    var image = s
      .appendSlide(SlidesApp.PredefinedLayout.BLANK)
      .insertImage(e);
      
    image.setTitle(e.getName());
    image.setDescription(new Date().toISOString());

    var imgWidth = image.getWidth();
    var imgHeight = image.getHeight();
    var pageWidth = s.getPageWidth();
    var pageHeight = s.getPageHeight();
    var newX = pageWidth / 2 - imgWidth / 2;
    var newY = pageHeight / 2 - imgHeight / 2;
    image.setLeft(newX).setTop(newY);
  });
  s.saveAndClose();
}

function main() {
  var dApp = DriveApp;
  var folder = dApp.getFolderById('***Folder with Images***');  //change this to the folderID where the images live
  var filesInter = folder.getFiles();
  var images = [];
  while (filesInter.hasNext()) {
    var file = filesInter.next();
    Utilities.newBlob(                                                        //I moved the conversion here. Don't know if it makes any difference.
          Utilities.base64Decode(file.getBlob()),
          file.getMimeType(),
          file.getName()
        )
    images.push(file);
  }
  appendImages(images); 
}

Darren D'Mello

unread,
Apr 10, 2022, 3:17:41 AM4/10/22
to Google Apps Script Community

--
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/08685ab3-aa8e-4a14-bb7a-10d50d48b865n%40googlegroups.com.


--
Best,
Darren

Michael Timpano

unread,
Aug 25, 2022, 12:54:31 PM8/25/22
to Google Apps Script Community
Thanks for the code it works great. I was wondering how the code can be modified to add 2 images per slide from a list of images in a google drive? Thanks again. Take care.

Janine Mariel Medina

unread,
Nov 27, 2022, 2:05:25 AM11/27/22
to Google Apps Script Community
Hi tried this but I'm getting

TypeError: Cannot read property 'forEach' of undefined
appendImages
@ Code.gs:3

Clark Lind

unread,
Nov 27, 2022, 9:41:16 AM11/27/22
to google-apps-sc...@googlegroups.com
Make sure you have access to the folder where the images are stored (folder shared with the account running the script), and you have the folderId added in the main() function. It looks like you do not have access to the folder, or didn't include the folderId.

CONFIDENTIALITY NOTICE: The contents of this email (including any attachments) are confidential and privileged and only intended for the recipient(s) addressed above. If you are not the intended recipient or have otherwise received this email by error, please notify the sender immediately and destroy it (and all attachments) without using, copying, storing and/or disseminating any of its contents (in any form) to any person. Email communication is not secure. foodpanda Philippines Inc. is not liable for any losses arising out of any errors or omissions in the contents resulting from email transmission or any illegal or unauthorized usage or tampering of its email system.

--
You received this message because you are subscribed to a topic in the Google Groups "Google Apps Script Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-script-community/SpeKW32I-AA/unsubscribe.
To unsubscribe from this group and all its topics, 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/42dab08f-f916-4f6d-af08-f1bb0fb4e1e2n%40googlegroups.com.

Brian Pugh

unread,
Nov 27, 2022, 9:47:55 AM11/27/22
to google-apps-sc...@googlegroups.com
I too am getting this error. Any idea why?

script error.jpg

















Brian Pugh, IT/Educational Technologies



Associated Hebrew Schools | Danilack Middle School

p: 416.494.7666, | e: bp...@ahschools.com

w: www.associatedhebrewschools.com

252 Finch Ave W., Toronto, ON M2R 1M9


facebook.png twitter.png instagram.png 


This email is confidential and is intended for the above-named recipient(s) only. If you are not the intended recipient, please delete this email from your system. Any unauthorized use or disclosure of this email is prohibited.




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/CAHgJr-0xfBA6OEitwOuupOTdMAEj8VRagq0-Ekdm%2BQumAZbJTg%40mail.gmail.com.

Clark Lind

unread,
Nov 27, 2022, 5:23:19 PM11/27/22
to google-apps-sc...@googlegroups.com
In the main() function, instead of calling the addImageSlide(images) function, comment that out and instead, add:

console.log(images.length)

and see if the images array has any content. That seems to be the problem.

Brian Pugh

unread,
Nov 27, 2022, 5:52:14 PM11/27/22
to google-apps-sc...@googlegroups.com
Thank you, Clark, but I get the same error.

Have I made the change incorrectly?

var folder = dApp.getFolderById('1pYk-c0mF************&&^&^^^4MleZUsoqn8b');
var filesInter = folder.getFiles();
var images = [];
while(filesInter.hasNext()){
images.push(filesInter.next());
} console.log(images.length)
//addImageSlide(images);//
}


















Brian Pugh, IT/Educational Technologies



Associated Hebrew Schools | Danilack Middle School

p: 416.494.7666, | e: bp...@ahschools.com

w: www.associatedhebrewschools.com

252 Finch Ave W., Toronto, ON M2R 1M9


facebook.png twitter.png instagram.png 


This email is confidential and is intended for the above-named recipient(s) only. If you are not the intended recipient, please delete this email from your system. Any unauthorized use or disclosure of this email is prohibited.



Michael O'Shaughnessy

unread,
Nov 27, 2022, 8:55:20 PM11/27/22
to google-apps-sc...@googlegroups.com
Just adding some info....

I just threw this little code together and it works... maybe it can shed some light on your issues:
const imageFun = ()=>{
let folder = DriveApp.getFolderById("FOLDER ID OF THE IMAGES")
let files = folder.getFiles()
let rslt = []
while (files.hasNext()){
let file = files.next()
rslt.push([file.getName(),file.getId()])
addImage(file.getId())
}
console.log(rslt)
}


const addImage = (theID)=>{
let presentation = SlidesApp.openById("PRESENTATION ID")
let image = DriveApp.getFileById(theID)
presentation.appendSlide().insertImage(image)
}

Brian Pugh

unread,
Nov 27, 2022, 10:06:59 PM11/27/22
to google-apps-sc...@googlegroups.com
Yes...it worked very quickly and perfectly!

Thank you very much, Michael.

Brian


















Brian Pugh, IT/Educational Technologies



Associated Hebrew Schools | Danilack Middle School

p: 416.494.7666, | e: bp...@ahschools.com

w: www.associatedhebrewschools.com

252 Finch Ave W., Toronto, ON M2R 1M9


facebook.png twitter.png instagram.png 


This email is confidential and is intended for the above-named recipient(s) only. If you are not the intended recipient, please delete this email from your system. Any unauthorized use or disclosure of this email is prohibited.



Janine Mariel Medina

unread,
Nov 28, 2022, 12:39:31 AM11/28/22
to Google Apps Script Community
it worked well! thank you!

Clark Lind

unread,
Nov 28, 2022, 6:46:15 AM11/28/22
to google-apps-sc...@googlegroups.com
When you run main from the apps script editor (debug, not run), does the console show any images.length more than 0?  

On Sun, Nov 27, 2022 at 5:52 PM Brian Pugh <bp...@ahschools.com> wrote:

cwl...@gmail.com

unread,
Nov 28, 2022, 6:59:30 AM11/28/22
to Google Apps Script Community
Thanks Michael, that is a simpler approach and if it works, then great!!  

Michael O'Shaughnessy

unread,
Nov 28, 2022, 10:51:10 AM11/28/22
to google-apps-sc...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages