Quantcast
Channel: Hot Weekly Questions - Web Applications Stack Exchange
Viewing all articles
Browse latest Browse all 9831

Need change Google Sheets to Google Drive Script to make sure the downloaded image has an extension included

$
0
0

This is a script I created using ChatGPT and it works but there is a small issue. The script checks a column called "downimg" to see if it contains the text "stop", if it does, the script will not skip the record, otherwise it will read the image URLs in the field "downloadurl" in the same Google Sheet and it will download the images and save them into Google Drive parent folder where it will create a subfolder first and place the downloaded images there. (The subfolder name will be generated from the text in 2 of the columns in the same Google Sheet).

The issue is that sometimes the downloaded images in Google Drive have no extension. It only happens 50% of the cases and it is quite random. I suspect it is because some images are JPG and others are PNG, the JPG images seem do not get the extension saved.

So that the resulting files have no extension, here is how they look. I solved this so far by running bulk rename and adding PNG extension to any image file and that fixes but it's an extra step that is unnecessary.

Question:What needs to be modified in the script to make sure the extension is included in the file? I tried to solve this issue again in ChatGPT and no luck so far.

Here is the Google Sheets Apps script:

function insertAndDownloadImages() {   let sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();    let lastRow= sheet.getLastRow();   for (let i = 0; i < lastRow-1; i++) {    let downimgValue = sheet.getRange(2+i, 120).getValue().trim().toLowerCase(); // Assuming "downimg" is in column 120    // Check if "downimg" is not empty and contains "stop"    if (downimgValue !== "" && downimgValue === "stop") {      Logger.log("Skipping row " + (2+i) +". downimg column contains 'stop'.");      continue;    }    let folderNameA = sheet.getRange(2+i, 3).getValue().trim();    let folderNameB = sheet.getRange(2+i, 2).getValue().trim();    let combinedFolderName = folderNameA +' - '+ folderNameB;    let urls = sheet.getRange(2+i, 107).getValue().split('\n'); // Assuming downloadurl is column 107    // Create a folder with the name from the first and second columns    let parentFolder = DriveApp.getFolderById("xxxxxxxx_folder_ID_comes_here_xxxxxxxx");     let subFolder = parentFolder.createFolder(combinedFolderName);    for (let j = 0; j < urls.length; j++) {      let url = urls[j].trim();      if (url !== "") {        let blob = UrlFetchApp.fetch(url).getBlob();         subFolder.createFile(blob);      }    }    // Update the Google Sheet in column 120 in downimg with "stop"    sheet.getRange(2+i, 120).setValue("stop");   } }

Here is the result

The image has no extension....

enter image description here


Viewing all articles
Browse latest Browse all 9831

Latest Images

Trending Articles



Latest Images