0
var rows = 13;
var columns = 1;
var ss = SpreadsheetApp.getActiveSpreadsheet();

function addRow() {
  var sheet = ss.getSheets()[0];
  var column_index = columns; // your column to resolve
  var cell = sheet.getRange(rows, columns, 1, 1);
  // Sets borders on the top and bottom, but leaves the left and right unchanged
  // Also sets the color to "red", and the border to "DASHED".
  cell.setBorder(true, true, true, true, true, true, "black", SpreadsheetApp.BorderStyle.SOLID);
  rows = rows +2

}

This is the script for a button. When the button is clicked, I want to create a border around the cell, 2 rows after the current row. However, when the button is clicked, the row variable remains 13, even though I have the rows - rows+2 at the end of each click.

Rubén
  • 34,714
  • 9
  • 70
  • 166
Tech333
  • 41
  • 3
  • Are you expecting this to permanently store this? For example `var rows = 15;` for your next run of the script? – ross May 14 '19 at 21:23
  • Try this `function myFunction() { var rg=SpreadsheetApp.getActiveRange().offset(2,0); rg.setBorder(true, true, true, true, true, true, "black", SpreadsheetApp.BorderStyle.SOLID); rg.activate(); }` – Cooper May 14 '19 at 21:59
  • There is no permanent instance of your code running. Every time it runs, `rows` is initialized per the specification in your code file--to the exact value of `13` – tehhowch May 14 '19 at 23:32

1 Answers1

0

Every time that a Google Apps Script is called by an UI element like a button, the whole project is loaded so the rows variable is initialized every time the button is clicked.

Besides other alternatives , you could use the Properties Service or the Cache Service to store variable values and pass them to other executions.

I'm not extending my answer because this was already explained on other Q/A on this site. Here a couple of examples:

Rubén
  • 34,714
  • 9
  • 70
  • 166