Home > Mobile >  How to download the list of urls of images with renaming them with the values available in adjacent
How to download the list of urls of images with renaming them with the values available in adjacent

Time:01-01

I've made the script in google sheets and code of gs script (code.gs) it has been working fine and nice with the download function it is downloading each and every image in my google drive folder. But the main thing is that I want them to be downloaded with renaming them in the mean time with the column of text values available on the adjacent column

I just want the images to be renamed in the mean time when they are being downloaded in the google drive folder.

for example FIRST COLUMN ==>> "PICTURE URL" SECOND COLUMN ==>> "TEXT_TO_BE_RENAMED"

I've made the download button, when I click the download button it should execute the command to download the image and as well as rename the image the the values available on second column. Example of it is shown in below screen shot. I'm also sharing the sheet link below:

[HERE IS THE PICUTRE DEMONSTRATION WHAT I I WANT]

enter image description here

https://docs.google.com/spreadsheets/d/1jItuI2tQbpH4A5b9CFS9xWV3mNtnrV2jMRYPxEZSvlM/edit?usp=sharing

My code:

function downloadImage() {
    let sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
    let lastRow = sheet.getLastRow();
    for (let i = 0; i < lastRow - 1; i  ) {
        let folder = DriveApp.getFolderById("Folder_id_of_your_drive");
        let url = sheet.getRange(2   i, 1).getValue();
        let image = SpreadsheetApp.newCellImage().setSourceUrl(url);
        let blob = UrlFetchApp.fetch(url).getBlob();
        folder.createFile(blob);
    }
}

I literally tried every thing to produce the result but unfortunately failed I am expecting the result that the images can be downloaded with the renamed text available in the separate list of cells values or with the adjacent columns

CodePudding user response:

You can check for errors and rename the files like this:

function downloadImage() {
  const folder = DriveApp.getFolderById('...folder ID goes here...');
  const sheet = SpreadsheetApp.getActiveSheet();
  const urls = sheet.getRange('A2:A').getDisplayValues().flat();
  const names = sheet.getRange('C2:C').getDisplayValues().flat();
  urls.forEach((url, index) => {
    if (!url) return;
    try {
      const blob = UrlFetchApp.fetch(url).getBlob();
      folder.createFile(blob).setName(names[index]);
    } catch (error) {
      console.log(error.message);
    }
  });
}

For additional information, see Folder.createFile() and the Blob class.

CodePudding user response:

function downloadandrename() {
  const f = DriveApp.getFolderById('fid');
  const sh = SpreadsheetApp.getActiveSheet();
  const vs = sh.getRange('A2:C'   sh.getLastRow()).getDisplayValues();
  vs.forEach((r, i) => {
    if (r[0] && r[2]) {
      f.createFile(UrlFetchApp.fetch(r[0]).getBlob().setName(r[2]));
    }
  });
}
  • Related