isk27
isk27

Reputation: 359

How to replace text by using Google scripts?

I want to show an image from Google Drive in a Google Spreadsheet. I have to replace the text: open? for: uc?export=download& in the Shareable link: https://drive.google.com/open?id=0BwJUnx7uETDmaG42eGdEVUVocU0

I have tried this solution https://stackoverflow.com/a/42819032/2661411:

" I made a two lines script to use the share link of an image in Google Drive.

  1. Go to Tools > Script editor.
  2. Copy, paste the following code
  3. Click on run for permission

    function DRIVE_IMAGE(link){ prefix_url = "https://docs.google.com/uc?export=download&"; link.replace("open?", "uc?export=download&"); }

Using the script :

  1. Copy the share link of your image in Google Drive.
  2. Go to a cell
  3. Enter the formula =IMAGE(DRIVE_IMAGE("COPIED_LINK")) "

But didn't work for me, it said:

TypeError: Cannot call method "replace" of undefined. (line 3, file "ImgView")

Any ideas? I would like it to keep it simple.

Upvotes: 0

Views: 832

Answers (1)

Ritesh Nair
Ritesh Nair

Reputation: 3355

You need to use https://docs.google.com/uc?export=download&id=<<IMAGE ID>>. Refer the below formula.

=image("https://docs.google.com/uc?export=download&id=0BwJUnx7uETDmaG42eGdEVUVocU0")

Script

/**
 * @customfunction
 */
function DRIVE_IMAGE(link){
   var newLink = link.replace("https://drive.google.com/open?", "https://docs.google.com/uc?export=download&");
   return newLink;
}

Now, you can use the formula =IMAGE(DRIVE_IMAGE("https://drive.google.com/open?id=0BwJUnx7uETDmaG42eGdEVUVocU0"))

Upvotes: 2

Related Questions