How to Save/download an "image" from an external service url, into "Google drive folder" ?

Dvd
Bronze 1
Bronze 1

Hi everyone,

I can't seem to find any information to figure out on how to Save/Download an image from an external url into my google drive in Appsheet.

I'm creating barcodes with an appsheet template "barcode generator" which uses an external service, that creates a link in the google sheet which is directly pointed to this service to create the barcode. for example 

https://barcode.tec-it.com/barcode.ashx?code=Code128&data=IZE0006&dpi=192&imagetype=PNG&rotation=0&e...

After creation of some barcodes, I created an automation to put the barcodes i can choose into a PDF file to be printed.
This works fine, but when I create several pdfs with several barcodes, I get a rate limit from this service, because it renders each time again the Barcode from the link in google sheet.

So I'm trying to figure out on how to save this image (from the link above), into my google drive folder called "BarcodeImages" --> So it renders the Barcode only once and saves it in the Google drive folder.

(This can be done manualy, But I'm trying to avoid manual work ๐Ÿ˜‰ )

Which then in return I can use them in Appsheet with no limitations into PDF files directly from the google drive folder. 

Can anyone help me out with this please.

Thanks already in advance, kind regards.

D.

 

 

1 4 402
4 REPLIES 4

Steve
Platinum 5
Platinum 5

Not possible with AppSheet directly.

Ok, pitty, thx for you time ๐Ÿ™‚



@Steve wrote:

Not possible with AppSheet directly.


But possible directly within AppSheet ๐Ÿ˜œ 

  • While there isn't a native thing you could use that would accomplish this, like an action or formula or something;  you could absolutely call a script through automation that would accomplish this.

Appster says:

Here's a Google Apps Script to download an image from an external URL and save it to a specified Google Drive folder. This script accepts a folder ID, the folder path where it will save the image (creating folders if necessary), a file name, and the image URL. 

 

/**
 * Downloads an image from a URL and saves it to a specified Google Drive folder.
 *
 * @PARAM {string} folderId - The ID of the base folder in Google Drive.
 * @PARAM {string} folderPath - The path inside the base folder where the image should be saved.
 *                              Subfolders will be created if they do not exist.
 * @PARAM {string} fileName - The name to save the file as (including the extension, e.g., "barcode.png").
 * @PARAM {string} imageUrl - The URL of the image to download.
 * @returns {GoogleAppsScript.Drive.File} - The saved file in Google Drive.
 */
function saveImageToDrive(folderId, folderPath, fileName, imageUrl) {
  try {
    // Get the base folder by ID
    let folder = DriveApp.getFolderById(folderId);

    // Split folderPath into an array of subfolder names
    const folders = folderPath.split('/').filter(Boolean);

    // Iterate over folders to create or navigate to subfolders
    folders.forEach(subfolderName => {
      const matchingFolders = folder.getFoldersByName(subfolderName);
      folder = matchingFolders.hasNext() ? matchingFolders.next() : folder.createFolder(subfolderName);
    });

    // Fetch image content from the URL
    const response = UrlFetchApp.fetch(imageUrl);
    const blob = response.getBlob().setName(fileName);

    // Save the image file in the final folder
    const file = folder.createFile(blob);
    return file;
  } catch (error) {
    console.error(`Error saving image to Drive: ${error.message}`);
    return null;
  }
}

 

Explanation:

  1. Base Folder Retrieval: Starts with a base folder specified by `folderId`.
  2. Folder Path Navigation: Splits the `folderPath` by `/` and iterates over each part to either find or create the necessary subfolder.
  3. Image Download and Save: Fetches the image from the provided `imageUrl`, converts it to a blob, and saves it as `fileName` in the final directory.

This function allows for automatic folder creation along a specified path, streamlining the process of saving generated barcodes in a structured way.

 Hope it helps!

___________________________________________________________________________________________
Next time, engage with Appster to get answers faster!
- You can chat with the LITE VERSION of Appster through ChatGPT.
- Gain access to the FULL VERSION by signing up at www.MultiTechVisions.com/answers (Main Menu > Support Tiers)

My wording was quite intentional. ๐Ÿ™‚

Steve_0-1731596789893.png

Top Labels in this Space