asantejava
asantejava

Reputation: 47

Google Apps Script getRange() Syntax

My apps script code works when I use:

sheet.getRange("B2:D7").setValue(10);

But it does not work when I use:

sheet.getRange("DTstart:DTend").setValue(10);

Although,

var DTstart = B2;
var DTend = D7;

How do I circumvent this error? Is there a way to get the reference for a cell assigned to a variable? The value of DTstart and DTend changes throughout the code.

Upvotes: 1

Views: 1625

Answers (1)

Tanaike
Tanaike

Reputation: 201428

  • You want to put the value using DTstart and DTend.
  • Values of DTstart and DTend are B2 and D7, respectively.

If my understanding is correct, how about this modification?

Modification points:

  • When B2 is used as a string, please modify to "B2"
  • When DTstart and DTend are used as the variables, please modify to DTstart + ":" + DTend instead of "DTstart:DTend".

Modified script:

var DTstart = "B2"; // Modified
var DTend = "D7"; // Modified
sheet.getRange(DTstart + ":" + DTend).setValue(10); // Modified

Note:

  • In above case, 10 is put to the cells of B2:D7.

If I misunderstood your question, I apologize.

Upvotes: 4

Related Questions