Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Add a shape with google app js in a sheet

40 views
Skip to first unread message

Hugues Malherbe

unread,
Oct 2, 2024, 1:58:04 AM10/2/24
to Google Apps Script Community
hello,
can you help me how to insert a shape (a rectangle for example) in a sheet
in an app script  js script project.
additionnaly i want to attach a function to the shape.
Gemini and copilot give codes not functionnal.
Drawings or Drawing objects are not available if google sheet API.

Thans for your support.

Hugues (from France)

Ed Robinson

unread,
Oct 2, 2024, 9:50:30 AM10/2/24
to Google Apps Script Community
Hi Hugues,
it sound like you want to programmatically insert a button into a sheet, and configure the button to execute a function.

Here is how to do this: 
1. Programmatically insert an image onto the sheet (shaped like a button), then
2. Programmatically assign a script to the image


Here is the code:
- Add the image of the button to your home folder, and name it "mybutton.png"
- Then run the addButton() function
- Viola! 



function addButton() {
var blob = DriveApp.getRootFolder().getFilesByName("mybutton.png").next().getBlob()
var sht = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()

var img = sht.insertImage(blob,1,1)
img.assignScript("helloWorld")
}
function helloWorld() {
SpreadsheetApp.getUi().alert("Hello world")
}


Reply all
Reply to author
Forward
0 new messages