0

I set formula in sheet1 cell (1,9) to get the value from other sheet through google apps script. Where "sheet" is a variable, I need to get value from specific sheets. The code below shows "=sheet!B1" rather than "=6!B1".

var ss1 = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/xxxxxxxxxxxxxxxxxxxx/edit#gid=0");
var sheet1 = ss1.getSheetByName("Sheet1");  
var sheet;
sheet = 6;
var sheet = sheet.toString();
sheet1.getRange(1,9).setFormula('sheet!B1')
}
Rubén
  • 34,714
  • 9
  • 70
  • 166
LookForward Chuang
  • 145
  • 1
  • 3
  • 12

1 Answers1

2
  1. Google Apps Script uses JavaScript as programming language
  2. setFormula requires a string as parameter.

Considering the above you could use any string manipulation technique available using the JavaScript version supported by the runtime that your project is using.

Among other techniques you coul use:

  1. String concatenation operator i.e. sheet + '!B1' (if you are using the old (Mozilla Rhino) or the new runtime (Chrome v8) )
  2. String templates i.e. `${sheet}!B1` (only if you are using the new runtime (Chrome V8) )
Rubén
  • 34,714
  • 9
  • 70
  • 166