Wayne S.
Wayne S.

Reputation: 21

using a variable within a string with Google Sheets

I am using Google Apps for Sheets. I am trying to use a defined variable within a string. I know the variable (lastRow) is the number I want (that number being "11") as I can see it in the logs. I have tried different ways of combining the letter "C" with the variable, but nothing works! I know it works as it is used in the "copyValuesToRange" method. Am I trying to do something that cannot be done, or is there a way to add the variable to the A1 notation so that the range will be read as C1:C11? Thanks from a relatively novice newbie!

    var lastRow = sheet.getLastRow();
    Logger.log(sheet.getLastRow());
    // Inserts 1 column after column A=1 (Seq)
    sheet.insertColumnsAfter(1,1); 
    // New column(s) due to added column(s)
    var range = sheet.getRange("C1:ClastRow");
     //Copied to Col A=1
    range.copyValuesToRange(sheet,1,1,1,lastRow);

While writing this, the "Similar Question" box showed a link to "Google script string with a variable". I looked at that, but did not understand it "(!

Upvotes: 2

Views: 4040

Answers (1)

ReyAnthonyRenacia
ReyAnthonyRenacia

Reputation: 17651

You dont do it like that, you need to know concatenation.

var lastRow = 11;
console.log("C"+lastRow);

will output:

C11

which is what you're going for.

 var range = sheet.getRange("C1:C"+lastRow);

Upvotes: 1

Related Questions