Replace URL with images inside a google doc table

331 views
Skip to first unread message

Diana Ramos Fusther Correa

unread,
Jan 30, 2023, 11:24:53 AM1/30/23
to Google Apps Script Community
So I have a report that gets filled with data from google sheets. 
I've managed to generate the tables In Google doc by copy/pasting a sorted table from sheets. The screenshot column should show images, these images are saved in a folder when a user fills in a form in AppSheet.

Screenshot 2023-01-30 102151.png
Is there a way to switch the URL for an image?

I tried searching for tutorials about this but most info I found is about google sheets and not google docs. D:


Halicate

unread,
Jan 30, 2023, 1:33:00 PM1/30/23
to Google Apps Script Community
Hi Diana,
of course you could built a process to obtain url, name and type of every file in your image folder doing sometingh like this:

function URLS() {
  var folder = DriveApp.getFolderById("1IHUutw0aelSXXXsJjLJhQ9iLVZtERFKb");  (put here your image Folder ID)
  let files = folder.getFiles()
  while(files.hasNext()){    
    let file = files.next()
    let result= [file.getUrl(),file.getName(),file.getMimeType()]
    Logger.log(result) 
  }  
}

After that, you could pair this list with names in your sheet an add an extra column with correspondent url and then, you were able to put urls into your doc, but... 
if someone open the sheet or the doc, to view a mini image or follow the link, is necessary authorization to view image folder.

I hope it helps!

Diana Ramos Fusther Correa

unread,
Jan 31, 2023, 5:16:37 PM1/31/23
to Google Apps Script Community
My bad, english is not my first language so I probably didn't explain myself so well. 
I already have the URL values. that point to the image folder in Google Drive.

Currently the tables I am generating look like this:
Screenshot 2023-01-30 102151.png

But what I want is this, where instead of showing the URL I have the image:
Untitled-1.png

Halicate

unread,
Feb 1, 2023, 6:07:26 AM2/1/23
to Google Apps Script Community
I'm afraid if someone has language problems, is me!!! I tought you wanted to transform names into URLs

Let me try again... perhaps I could do it better in a second chance:

Before execution:
Capture1.png

function URLS() {
  var ss = SpreadsheetApp.openById("1De5CEDGgwsfymJAY_SKimclOmfslcPoP9p29WSXQE6A"); // sample sheet
  var sheet1 = ss.getSheetByName("Names");
  var sheet2 = ss.getSheetByName("Images");
  sheet2.setColumnWidth(1, 500);
  for (var i = 1; i < sheet1.getLastRow(); i++) {
    var ImgName = sheet1.getSheetValues(i+1,1,1,1)    
    var folder = DriveApp.getFolderById("1IHUutw0aelXXXsJjLJhQ9iLVZtERFKb");  //put here your images folder ID
    let files = folder.getFiles()
    while(files.hasNext()){    
      let file = files.next()      
      if (file.getName() == ImgName[0][0]) {          
        const url = Drive.Files.get(file.getId()).thumbnailLink.replace(/\=s.+/,"=s512");                 
        const image = SpreadsheetApp.newCellImage().setSourceUrl(url).build();        
        sheet2.getRange(i+1,1).setValue(image);         
        sheet2.setRowHeight(i+1, 200);          
        break;  
      } 
    }    
  }

After execution, second sheets looks like this: 
Capture2.png


As you see, I took your images. Notice that code uses Google Api services to obtain a thumbnail image.
Of course if you have a lot of images you should make something more sofisticated, ordering rows and files for a better performance, but I hope this would be finally usefull.

Regards! 
Reply all
Reply to author
Forward
0 new messages