Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You can use Google Apps Script to upload images to Google Drive and retrieve the URL of the uploaded image, then populate the URL into a Spreadsheet.

Here are the steps you can follow:

  1. Create a new Google Spreadsheet.

  2. Open the Script Editor from Tools > Script Editor.

  3. Write the below code:

function uploadImage() {
  var folderName = "Images"; // Replace "Images" with your folder name in Google Drive
  var folder = DriveApp.getFoldersByName(folderName).next(); // Get the folder by name

  // Show the file upload dialog box
  var ui = SpreadsheetApp.getUi();
  var response = ui.prompt("Please enter the URL of the image to be uploaded:", ui.ButtonSet.OK_CANCEL);

  if (response.getSelectedButton() == ui.Button.OK) {
    var imageURL = response.getResponseText();

    // Create an image file and upload it to the folder
    var blob = UrlFetchApp.fetch(imageURL).getBlob();
    var file = folder.createFile(blob);

    // Get the URL of the uploaded file
    var fileURL = file.getUrl();

    // Populate the URL in the active cell of the Spreadsheet
    var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
    var activeCell = sheet.getActiveCell();
    activeCell.setValue(fileURL);
  }
}

  1. Save the script and give it a name, for example, ‘ImageUploader’.

  2. Go back to the Spreadsheet and select a cell in a column where you want to store the image URLs.

  3. From the menu, go to Tools > Macros > ImageUploader.

  4. A dialog box will appear that prompts you to enter the URL of the image you want to upload. Enter the URL and click OK.

  5. The image will be uploaded to the folder specified in the code, and the URL of the uploaded image will be populated in the active cell of the Spreadsheet.

You can repeat the process for as many images as you want to upload.