Advertisement
RemcoE33

=Image from drive

Jun 3rd, 2021
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function manual() {
  2.   const ui = SpreadsheetApp.getUi();
  3.   const driveFolder = ui.prompt("Enter google drive folder id").getResponseText().trim()
  4.   const imageType = `image/${ui.prompt("Enter image type: (png / jpeg / gif / svg").getResponseText().toLowerCase().trim()}`
  5.   const mode = Number(ui.prompt("Image mode ( https://support.google.com/docs/answer/3093333?hl=en )").getResponseText().trim());
  6.   const size = (mode == 4) ? ui.prompt("Set height and width comma separated (63,44)").getResponseText().trim() : null;
  7.   const onOff = ui.prompt("If you want a on / off switch enter a cell notation (A1) if not leave blank").getResponseText().trim();
  8.   const images = DriveApp.getFolderById(driveFolder).getFilesByType(imageType);
  9.  
  10.   _processImages(images, mode, onOff, size);
  11.  
  12. }
  13.  
  14. function _processImages(images, mode, onOff, size) {
  15.   const output = [];
  16.   const heightWith = (size) ? size.split(",") : null
  17.  
  18.   while (images.hasNext()) {
  19.     const file = images.next();
  20.     file.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW)
  21.     const downloadUrl = file.getDownloadUrl();
  22.     if (onOff) {
  23.       output.push([`=IF(${onOff} = TRUE,IMAGE("${downloadUrl}",${mode}${(size) ? `,${heightWith[0]},${heightWith[1]}` : ''})`])
  24.     } else {
  25.       output.push([`=IMAGE("${downloadUrl}",${mode}${(size) ? `,${heightWith[0]},${heightWith[1]}` : ''})`])
  26.     }
  27.   }
  28.   if (onOff) {
  29.     SpreadsheetApp.getActiveSheet().getRange(1, 1).insertCheckboxes();
  30.     SpreadsheetApp.getActiveSheet().getRange(2, 1, output.length, 1).setFormulas(output);
  31.   } else {
  32.     SpreadsheetApp.getActiveSheet().getRange(1, 1, output.length, 1).setFormulas(output);
  33.   }
  34.   SpreadsheetApp.getUi().alert(`Processed ${output.length} images`)
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement