-2

my frient shared his google sheet to me and the table contains image which is a link (url). How can i make a copy of this sheet and make all the image link to be local, so i want the image is copying to my local google drive automatically (so the link won't be broken if he delete his images files in future). Right now, if i make a copy of this document, then it still link to original image source.

How is it possible ? of course i don't want to manually copy them one by one from the link. Is there any better and faster way ?

https://docs.google.com/spreadsheets/d/1TkXwAd8rKbjnGfYEJVaOYBJwCZ7G7YfuSvmcDE6g8No/edit?usp=sharing

andio
  • 1,574
  • 9
  • 26
  • 45
  • Please share the spreadsheet. – Tedinoz Sep 26 '19 at 23:45
  • Some questions, please. Please update your spreadsheets to reflect the size and location of the table. Is this a one-time exercise, or something to be repeated regularly? Do you have experience using scripts? – Tedinoz Sep 30 '19 at 17:00

1 Answers1

0

The OP wants to extract the image URL from a hyperlink formula, and save a copy of the image to their own Google Drive account.

This answer combines several elements from precedents on StackOverflow.


function so5811567402() {
  var ss=SpreadsheetApp.getActiveSpreadsheet();
  var sheetName = "Table";
  var sh = ss.getSheetByName(sheetName);
  var rg=sh.getDataRange();
  var lastColumn = sh.getLastColumn();
  var lastRow = sh.getLastRow();

  var formulas = rg.getFormulas();

  for (var i in formulas) {
    for (var j in formulas[i]) {
      var formula = formulas[i][j];
      if (formula.length !=0){

        var regex = /image\("(.*)"/i;
        var matches = formula.match(regex);
        var imgurl = matches[1];
        var filename = imgurl.substring(imgurl.lastIndexOf("/") + 1, imgurl.lastIndexOf("."));
        //Logger.log(filename);

        var image = UrlFetchApp.fetch(imgurl).getBlob().getAs('image/jpeg').setName(filename);

        var FolderId = "Folder ID goes here";
        var folder = DriveApp.getFolderById(FolderId);
        var file = DriveApp.createFile(image);
        Drive.Files.update({"parents": [{"id": folder.getId()}]}, file.getId());
      }
    }
  }
}
Tedinoz
  • 5,911
  • 3
  • 25
  • 35