johnvan
johnvan

Reputation: 107

Can I get name of cell by range in app script google sheet

I have code to calculate two cells by formula this is my code :

function myFunction() {
     var ss = SpreadsheetApp.getActiveSpreadsheet();
     var sh = ss.getSheetByName("sheet29");
     var maxR = sh.getMaxRows();

     var first = sh.getRange(maxR,6,1);
     var second = sh.getRange(maxR,7,1);

     var calculate = sh.getRange(maxR,8,1);

     first.setValue("2");
     second.setValue("2");

     var q = "F6"; // I want to change it to some thing like var q = first...;
     var qq =  "G6"; // I want to change it to some thing like var q = second...;

     var formula= ("=sum("+q+"+"+qq+")");
     calculate.setValue(formula);
 }

This code is fine calculate by formula by this code

var formula= ("=sum("+q+"+"+qq+")");
calculate.setValue(formula);

what I need, I don't know what is the name of cells to calculate, this dynamic. Any way to find the name of cell by range or another method

Upvotes: 0

Views: 68

Answers (1)

Nikko J.
Nikko J.

Reputation: 5533

You can get the A1 notation of a range by using range.getA1Notation() method and use it to set formula. Change your code

From:

 var q = "F6";
 var qq =  "G6";

To:

 var q = first.getA1Notation();
 var qq =  second.getA1Notation();

Reference:

Upvotes: 1

Related Questions