简体   繁体   中英

Addressing individual cells in Google Sheets

Not sure it can be done but here is the question. I have a sheets app with multiple sheets. Sheet 1 contains data as follows:

String Descriptor | Value 1 | Value 2

Sheet 2 is working formulas. Cell D37 in this example has a reference to 'Sheet1'!A14

On sheet 2, column E I'd like to reference back to 'Sheet1'!A16

If I pass the string value of "D37" I can find the cell and then manipulate things and return the proper data. I can't however get things to work when I try to get the cell address passed by the function I get "range not found" errors.

Sheet 2 reference is '=INDIRECT(myFunction(D37))

function myFunction(piNameCell) { //piNameCell has the reference to D37

  var ss = SpreadsheetApp;
  var sheet = ss.getActiveSheet().getRange(piNameCell);  // Like this it crashes
  //var sheet = ss.getActiveSheet().getRange("D37");       //This works
  var sheetRef = sheet.getFormula();

  //remove the leading '=' sign
  sheetRef = sheetRef.substring(1);

  //find the !
  var bangIndex = sheetRef.indexOf("!");

  //change the column to 'E'
 sheetRef = sheetRef.substring(0,bangIndex+1) + "E" + sheetRef.substring(bangIndex+2)

  return sheetRef;
}

Like you said in your questions, you need to send the adress of the cell, not the content, but you don't want to send it as a string, because you want it to change if you move the formula. You have some options.

Get the cell coordinates

You could simply get the cell coordinates with the ROW and COLUMN functions.
You'd have to change a little your function to use the coordinates instead. Your function call could look like that:

=INDIRECT(myFunction(ROW(D37), COLUMN(D37)));

And in your function you'd change the parameters and the getRange function.

Use the cell coordinates to get the A1 notation

If you really want to send that A1 notation, you can do that with ADDRESS .

=INDIRECT(myFunction(ADRESS(ROW(D37), COLUMN(D37))));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM