1

Is it possible to use an Arrayformula to populate the entire column on the formula "getNotes"? https://developers.google.com/apps-script/reference/spreadsheet/range#getNotes()

So far I've added the small function on Apps Script as follows:

function getNotes(cell)
{
   var ss = SpreadsheetApp.getActiveSpreadsheet();
   var range = ss.getRange(cell)
   return range.getNotes();   
}

Then populated the column's first cell as follows:

=ARRAYFORMULA(IFERROR(getNotes(Address(row(B1:B), column(B1:B)))))

which does not seem to work. For the record,

=getNotes(Address(row(B2:B), column(B2:B)))

does work fine.

Marios
  • 26,333
  • 8
  • 32
  • 52
  • In your script, for example, when `=getNotes("B2:B")` is put to a cell, will the result be the same with your goal? – Tanaike Dec 31 '20 at 02:03
  • My goal is for the formula to get the cell's notes to populate the respective cell in its column. For example, the notes in the cell B2 should populate the P2 cell, B10 to P10, and so on. I just thought that since ArrayFormula took the range from B1:B, that would be the way to go. If that's not the case, I would be grateful to see the correct approach. – user3245228 Dec 31 '20 at 02:48
  • Added a part 2 to the question here as per advise of the answerer for future posterity: https://stackoverflow.com/questions/65524033/dynamically-autopopulate-column-with-getnotes – user3245228 Dec 31 '20 at 17:41

1 Answers1

2

Updated answer based on your comment:

If you want to transpose the full range of columns and pass the full column B "B1:B", you can use this instead:

function getNotes(rng){
  const ss = SpreadsheetApp.getActive().getActiveSheet();
  const index = ss.getMaxRows()-ss.getRange(rng).getNotes().flat().reverse().findIndex(v=>v!='');
  const range = ss.getRange(rng+index);
  return range.getNotes();
}

Essentially, this script will find the last cell that has a note and return until that cell instead of the full range:

updated answer

You can also use getNotes("B1:B") which will work in the same way, but it will spread the values in a column instead of a row.

Explanation:

As Tanaike mentioned in his comment, your current code:

function getNotes(cell)
{
   var ss = SpreadsheetApp.getActiveSpreadsheet();
   var range = ss.getRange(cell)
   return range.getNotes();   
}

requires to pass the desired range as string:

=getNotes("B1:B")

enter image description here

You don't need an arrayformula as this custom function returns an array itself.

You can then do use built-in google sheets formulas if you want, like transpose:

example

Alternative approach:

You can use the getRange(row, column, numRows, numColumns) method of the sheet object instead which allows you to pass numbers if you want:

function getNotes(rs,cs,re,ce)
{
   var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
   var range = ss.getRange(rs,cs,re,ce);
   return range.getNotes();
}

and then call it like that =getNotes(1,2,3,1):

example2

This is a useful link to see how this getRange method works.

Marios
  • 26,333
  • 8
  • 32
  • 52
  • I have tried using transpose as you mentioned, but I run into some problems. [Here](https://i.imgur.com/sT6IMzG.png) is what it currently looks like. None of the cells below get populated while the first cell tosses an error at me. I think I may have forgotten to include a key info, and that is I want to autopopulate the cells with the results of the formula as I arbitrarily create new rows, like the ArrayFormula. – user3245228 Dec 31 '20 at 02:53
  • @user3245228 see my updated answer. You have 2 options, you can transpose a specific range (as I showed in my answer - `"B1:B10"`) or use my script which finds the last cell with a note. I have now given you 2 options to work with :) – Marios Dec 31 '20 at 03:35
  • This answer was a huge step in the right direction, much appreciated. There's still one more major issue that needs to be tackled; unlike ARRAYFORMULA, when you sort the list in an arbitrary fashion via any criteria, the notes don't follow the cells, they remain in the cells where they originally populated. Is it possible to make them do that? And as a quick side issue, I wanted to label the cell with the formula using `IF(ROW(A:A)=1,"Label",getNotes("B1:B"))`, but that doesn't seem to work. Is there a way to label it and prevent that? This cell is in the same column (first row) the notes are. – user3245228 Dec 31 '20 at 17:09
  • 1
    I have marked your answer as accepted and posted a new one here: https://stackoverflow.com/questions/65524033/dynamically-autopopulate-column-with-getnotes Thank you for your help. – user3245228 Dec 31 '20 at 17:40
  • @user3245228 hi I just saw your question was answered. I am very glad. – Marios Dec 31 '20 at 20:49