1

I attempted to insert jpg images stored in a Google Drive folder into a spreadsheet using the script below. The Url I get using the getUrl() method does not work. However if I use this url to open the image in Chrome and right click on the image and choose 'Get image URL,' I get a Url that does work. Is there a script method that will get me the correct Url? Or is there another way of accomplishing the same result?

function testInsertImage() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];
  var folder = DocsList.getFolder('DataBasePicts');
  var files = folder.getFiles(); 
  var img = files[0].getUrl();
  sheet.insertImage(img, 2, 2);  // In Class Sheet method 'insertImage(url, column, row)'
}

// Url obtained by the .getUrl() above which does not work
// "https://docs.google.com/open?id=1fxx_KYV46swKQk5vh9h1ideOhW76ZhJVYIPUjopbXm4"
// Url obtained by right clicking the image when opened in Chrome using above Url which does work
// "https://lh6.googleusercontent.com/eWA2oIabdGeXLnIRkTkdXuZFlvt6L_pJbgKBLoTFVDEWVESPxpvziHJnFpeXocMmnwUEvYWIab4=w1318-h612"
//.insertImage gives this error message:
// Error retrieving image from URL or bad URL: https://docs.google.com/open?
2
  • What does file[0].getUrl() return? Does it return an empty string or give you some kind of error? Commented Nov 12, 2014 at 4:11
  • Error retrieving image from URL or bad URL: docs.google.com/open? Commented Nov 12, 2014 at 17:44

2 Answers 2

1

The Advanced Drive Service enables use of the Google Drive Web API from Google Apps Script. Once you've enabled the service by following these instructions, its methods and properties will show up in the GAS editor's Autocomplete feature, making it easy to explore the available capabilities.

For instance, an ADS File object has a collection of properties documented here. The one you're interested in is .webContentLink. We can easily combine ADS with the DriveApp methods that replaced the deprecated DocsList, for instance by retrieving the fileId of the image you're interested in and using it with the ADS get() function.

function testInsertImage() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];
  var folders = DriveApp.getFoldersByName('DataBasePicts');
  if (folders.hasNext()) {
    // Assume folder name is unique, so use first match
    var folder = folders.next();

    var files = folder.getFiles(); 
    if (files.hasNext()) {
      // For this test, use first found file
      var file = files.next();
      var img = Drive.Files.get(file.getId()).webContentLink;
      sheet.insertImage(img, 2, 2);  // In Class Sheet method 'insertImage(url, column, row)'
    }
    // else error: no file found
  }
  // else error: no folder found
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much Mogsdad. Took only a few minutes to fix and it worked perfectly. A very professional and compete answer.
0

use getWebContentLink() method of File.

File file = serive.files().get(fileId).execute();
file.getWebContentLink()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.