PrintSeer
PrintSeer

Reputation: 23

How to make a script in GAS that will create a button or its equivalent in a Google Sheet?

I have a google sheet with a variable amount of data, up to 1000 rows. I want to make a script that will create buttons in the spreadsheet for each row of data. Each button will trigger a script that will take values from a range in row the button is in and apply them to a Google Docs template.

The part I'm having problems with is creating the buttons with a script and passing arguments to the script, so that it will use the right row for each.

Is that even possible, or am I better off making a single button and modify the script to run through every row and create all the documents in one go?

Upvotes: 0

Views: 211

Answers (1)

ziganotschka
ziganotschka

Reputation: 26806

You cannot create nuttons programmatically, but you can do so with checkboxes.

In combination with an onEdit trigger you can implement that every click of a checkbox will retrieve the row where this checkbox is located and will take this row as a function parameter.

Sample:

var checkBoxColumn=5;

function initialSetup(){
 var sheet=SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
 var lastRow=sheet.getLastRow()
 var range=sheet.getRange(1, checkBoxColumn, lastRow);

 // insert a checkbox in every row
 var enforceCheckbox = SpreadsheetApp.newDataValidation();
 enforceCheckbox.requireCheckbox();
 enforceCheckbox.build();
 range.setDataValidation(enforceCheckbox).setValue("FALSE")
}  

function onEdit(e) {
 if(e.range.getColumn()==checkBoxColumn&&e.range.getValue()==true){
   var row=e.range.getRow();
   copyData(row);
 }
}

function copyData(row){
 Logger.log("The checkbox was clicked in row "+row);
 // do whatever you want with this row
}

Upvotes: 1

Related Questions