Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
210 views
in Technique[技术] by (71.8m points)

google apps script - Get notes from a cell and insert into another cell

I have a range of cells in Google Sheets, some of them have notes attached.

If there's a note attached to a cell, I need to put the note in a separate cell, and put the location of that note in another cell.

I found this script elsewhere:

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

but when I try to use it I get an error "Exception: Range not found (line 12)."

But this script only gets me halfway there as it only gets the note and puts it in a cell. I also need to know what cell the note came from.

Any help is greatly appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In the script above the function getNote() expects the paramter cell

If you just run the script without calling getNote() from another function / an environment where it gets a values for cell assigned, the script will fail wiht the error you obtained.

Indeed, this script doe snot not meet your needs. What you probably want is to screen all your cells for the one that have notes.

What you need to decide is into which cells you want to put the note and the cell notation.

Below is a sample that you need to adapt for your needs:

function getNotes() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  //if you have only one sheet in the spreadsheet, otherwise use ss.getSheetByName(name);
  var sheet = ss.getActiveSheet();
  var range = sheet.getDataRange();  
  var results = range.getNotes();  
  for (var i = 0; i < results.length; i++) {
    for (var j = 0; j < results[0].length; j++) {
      //if a not empty note was found:
      if(results[i][j]){
        var note = results[i][j];
        var cell = range.getCell(i+1, j+1);
        var notation = cell.getA1Notation();
        //adjust the offset as function of the column / row where you want to output the results
        cell.offset(0, 1).setValue(note);
        cell.offset(0, 2).setValue(notation);
      }
    }
  }
}

Important references:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...