Convert an image into an url button in Google Sheets Using Apps Script

2,123 views
Skip to first unread message

Iftikhar Omar

unread,
Jan 15, 2021, 12:01:07 AM1/15/21
to Google Apps Script Community

Hello,

Peace Be Upon You,


I need some help with google app scripts.


What I want to do:

  1. I want to add an image in google sheets

  2. Add an app script to this image

  3. Configure the script so that when I click the image it takes me to a url (external or internal)


What Help I need: 

I'm a beginner and would like to get the work done. 

Please help me with a sample or template code to place the url.


Why app script for this simple task:

Google sheets kept no other way to point a drawing or image to a url.


I’m new here. Please forgive me if asking this way is against manners. But please help me. 


Thanks,

Md. Iftikhar Alam Omar

https://iftikharomar.co


Boris Baublys

unread,
Jan 15, 2021, 4:08:28 AM1/15/21
to Google Apps Script Community
Hello. Do you want to add images to a sheet using a script? Or do you add an image to the sheet manually and you need a code that, when you click on this image, will open the required URL?  

пятница, 15 января 2021 г. в 08:01:07 UTC+3, Iftikhar Omar:

Iftikhar Omar

unread,
Jan 15, 2021, 4:56:41 AM1/15/21
to Google Apps Script Community
Thanks a lot for prompt reply.

I want to add images to sheet manually.

And then I need a code that, when I click on this image, will open the required URL <3

Boris Baublys

unread,
Jan 15, 2021, 5:35:17 AM1/15/21
to Google Apps Script Community
Thanks now, I understand. Use the following code. Assign the script  run to the image. Correct the url in the script to the desired one. On first launch, allow pop-ups for this url (top right in the address bar).  

function run(){
  var url = 'https://google.com';
  openUrl( url );
}

/**
 * Open a URL in a new tab.
 */
function openUrl(url){
  var html = HtmlService.createHtmlOutput('<html><script>'
  +'window.close = function(){window.setTimeout(function(){google.script.host.close()},9)};'
  +'var a = document.createElement("a"); a.href="'+url+'"; a.target="_blank";'
  +'if(document.createEvent){'
  +'  var event=document.createEvent("MouseEvents");'
  +'  if(navigator.userAgent.toLowerCase().indexOf("firefox")>-1){window.document.body.append(a)}'                          
  +'  event.initEvent("click",true,true); a.dispatchEvent(event);'
  +'}else{ a.click() }'
  +'close();'
  +'</script>'
  // Offer URL as clickable link in case above code fails.
  +'<body style="word-break:break-word;font-family:sans-serif;">Failed to open automatically. <a href="'+url+'" target="_blank" onclick="window.close()">Click here to proceed</a>.</body>'
  +'<script>google.script.host.setHeight(40);google.script.host.setWidth(410)</script>'
  +'</html>')
  .setWidth( 90 ).setHeight( 1 );
  SpreadsheetApp.getUi().showModalDialog( html, "Opening ..." );
}

пятница, 15 января 2021 г. в 12:56:41 UTC+3, Iftikhar Omar:

Iftikhar Omar

unread,
Jan 15, 2021, 6:08:51 AM1/15/21
to google-apps-sc...@googlegroups.com
Thanks a lot for this help.. :)


--
You received this message because you are subscribed to a topic in the Google Groups "Google Apps Script Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-script-community/xgI5VyCGhQ4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/04ad2302-f832-4341-b1e0-656b108ca00dn%40googlegroups.com.

Iftikhar Omar

unread,
Jan 15, 2021, 6:09:51 AM1/15/21
to Google Apps Script Community
Thanks a lot Mr. Boris.. :)

Boris Baublys

unread,
Jan 15, 2021, 1:09:17 PM1/15/21
to google-apps-sc...@googlegroups.com
You are welcome, Mr. Omar :-)

пт, 15 янв. 2021 г. в 14:09, Iftikhar Omar <md.ifti...@gmail.com>:
--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/d9b4fe8e-d97f-4f75-8050-41a0efae8819n%40googlegroups.com.

Sunay Sabri

unread,
Jul 17, 2022, 4:10:39 AM7/17/22
to Google Apps Script Community
Working perfect. Thanks a lot.
Reply all
Reply to author
Forward
0 new messages