How to use a variable in a string in Google Sheets Script Editor?

1.2k views Asked by At

I'm a newbie to Script Editor, so I need some help.

function makeitright(){
var app1 = SpreadsheetApp;
var ss1 = app1.getActiveSpreadsheet();
var cred = ss1.getSheetByName("Crédito");
var lastrow = cred.getLastRow(); 

var formmod = 'ISBLANK('"H"+(lastrow+1)')'')';

I'm trying to write a formula to a single cell. But this formula will always change with a new added row. So I need the formula to work with the variable that gets the lastrow + 1.

Any ideas in how this could be made?

2

There are 2 answers

0
Wicket On BEST ANSWER

replace

'ISBLANK('"H"+(lastrow+1)')'')';

by

 'ISBLANK(H' + ( lastrow + 1 ) + ')';

To learn more read about how string concatenation works in JavaScript.

0
r.n. hermann On

A more elegant way would be to use JavaScript Template Literal syntax:

`ISBLANK(H${lastrow + 1})`;