2

Google Drive files are shared out via a unique (and random) URL when uploaded. Is there a way to upload an image via a Sheets formula that uses the file path of the image, NOT the sharing URL?

Instead of using: https://drive.google.com/file/(sharing link)

The formula would use something like: Drive/Test/img.png or Drive/Test/img.gif

I have noticed that within the help for the IMAGE function in Google Sheets it explicitly states that you cannot use images hosted at drive.google.com but I'd like to know if there's another way to accomplish this.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Falcon4ch
  • 152
  • 1
  • 1
  • 12

2 Answers2

2

If you want to get a specific image in a specific folder, you will have to explore your drive as multiple folders and multiple files can have the same name.

function listOfFilesOfFolder() {
  var myFolder = 'yourFolder';
  var sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('yourSheet');
  sh.clear();
  sh.appendRow(["name", "date", "URL", "id", "type"]);
  var folders = DriveApp.getFoldersByName(myFolder)
  var foldersnext = folders.next();
  var data = [];
  var files = foldersnext.getFiles();
  while (files.hasNext()) {
    var file = files.next();
    data = [ 
      file.getName(),
      file.getLastUpdated(),
      file.getUrl(),
      file.getId(),
      file.getMimeType()
    ];
    sh.appendRow(data);
  }
  sh.getRange('F1').setFormula(`={"image";arrayformula(if(D2:D="",,if(left(E2:E,5)="image",IMAGE("https://docs.google.com/uc?export=view&id="&D2:D),)))}`)
}
Mike Steelson
  • 14,650
  • 2
  • 5
  • 20
2

Unfortunately this isn’t possible. As Google Drive supports the existence of multiple files with the same name in the same folder, a file path isn’t enough to uniquely identify a file and so the file ID is required regardless of whether it’s ‘file path’ is unique or not.

Rafa Guillermo
  • 14,474
  • 3
  • 18
  • 54