Getting comments from range in Google Script

2019-08-16 13:16发布

问题:

I'm trying to parse a Google spreadsheet, that holds comments within merged cells.

I'm trying to extract and create 'shift' objects from the range:

/**    
* Return shifts object
* @contextSheet the sheet that is being parsed
* @baseTime the relative time that represent day 1
*/
extractShiftsFromSpreadSheet : function (contextSheet,baseTime) {
  var shifts = [];
  var ss = contextSheet;
  //For each day of the week:
  for(var dayIndex = 1; dayIndex <7 ; dayIndex++) {        
    var day = Script.Util.getDayByNum(dayIndex-1);  //Convert day num to day string
    var ShiftRangePerDay = Script.SpreadSheet.getShiftRangeByDay(ss,day);
    var notes = ShiftRangePerDay.getComments();
    var rows = ShiftRangePerDay.getNumRows();
    var cols = ShiftRangePerDay.getNumColumns();
    var values = ShiftRangePerDay.getValues();
    var startTime;
    var endTime;
    var note = '';
    for (i = 0; i <= rows - 1; i++) {
      for (j = 0; j <= cols - 1; j++) {
        //var cell = values[i][j];
        var studentName = values[i][j].trim();
        //if the cell value isn't a student, move on to the next cell
        if(!Script.Util.isValidStudent(studentName)) continue;
        // otherwise, it is a valid student shift: create it!

        try {
          note = notes[i][j];
        }
        catch(err) {
          //skip

        }
        if(note !== '' && typeof note !== 'undefined') {
          Logger.log("note found for "+studentName+"."  +" note:"+ note);
        }

now, the var notes = ShiftRangePerDay.getComments(); line should return a String[][] object and I should be able to read from it, according to the following API reference: https://developers.google.com/apps-script/reference/spreadsheet/range?hl=en#getComments()

However, it does not work. I've tried using getComments() , getNotes() and even getCell(i,j).getNote(). Nothing seems to be working here.

It seems that I've missed something here, and I would love to hear what you think of it.

回答1:

The methods dealing with comments are broken, as reported in Issue 2566. Star it to receive updates, and to help drive up the priority of a fix.

If you use Notes (not Comments), you should find that your code works. Notes are less user-friendly in the Sheets UI, but at least they (sorta) work with scripts.