Guilherme Lopes
Guilherme Lopes

Reputation: 27

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

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?

Upvotes: 1

Views: 1629

Answers (2)

r.n. hermann
r.n. hermann

Reputation: 43

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

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

Upvotes: 0

Wicket
Wicket

Reputation: 38425

replace

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

by

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

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

Upvotes: 2

Related Questions