Google-sheets – How to reference a cell with notes still attached in a Google Spreadsheet

google sheetsgoogle-apps-script

I'm simply looking for how to link one cell's data with the notes still attached into another cell and/or sheet.

With my code I cant't seem to figure out how to reference a single cell from another sheet with the notes still attached. This is the best code I have about it. I get error that range is not found. I'm using =copyNote(Ref!B59)

function copyNote(cell) {
  return SpreadsheetApp.getActiveSheet().getRange(cell).getNote();
}

Best Answer

Short answer

It's not possible to use a custom function to make that a cell shows the referenced cell value and note. Instead use function to copy the cell.

Explanation

Custom functions can only return values, they can't modify cell attributes like cell notes.

References in custom functions

Instead of Ref!B59 use "Ref!59" or cell("address",Ref!B59). The final formula will be =copyNote("Ref!B59") or =copyNote(cell("address",Ref!B59))

The above will return the value of the note from Ref!B59.

A more elaborated approach is to use getFormula() and regular expressions to extract the reference from the formula.

Workarounds

Using a script to copy a cells

The following function will copy a fixed cell to another fixed cell including. This will include notes.

function copyCell() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var origin = ss.getRange('A1');
  var destination = ss.getRange('F1');
  origin.copyTo(destination);
}

Using custom functions

In case that a custom function should be used, to get the value and the note from a cell reference, some changes should be done to the function. The following code returns a 1X2 array, the first resulting cell will contain the referred cell value and the second resulting cell the referred cell note:

function copyValueNote(cell) {
  var rng = SpreadsheetApp.getActiveSheet().getRange(cell);  
  return [['Value: ' + rng.getValue(),'Note: ' + rng.getNote()]];
}