3
votes

I am working to programmatically display a list of residents for a "know your neighbors map" community project. I want to take any number of first & last names for a residence and list them with last names in bold and leading any first names & numbers. The kludge code below or from the link is my first pass. I have tried various iterations to get the exception to go away, but I have been unsuccessful and I can't find anything concrete in the documentation or on the Googles.

Make a copy and run it yourself with this link: https://docs.google.com/spreadsheets/d/1rWHG5CKHvFzohTq9fSl8p8AjNcFjX0Tatior_hvOncE/copy

I'm getting the following error: "Exception: The parameters (SpreadsheetApp.RichTextValueBuilder) don't match the method signature for SpreadsheetApp.Range.setRichTextValue. (line 49, file "Code")"

Any ideas?

Kludge down here!

function main() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var dataSheet = ss.getSheetByName('Data');
  var address_array = dataSheet.getRange('AddressData').getValues();
  var bold = SpreadsheetApp.newTextStyle().setBold(true).build();
  var resident_count = 1;
  for (var i = 0; i < address_array.length; i++) {
    var resident_array = address_array[i];
    var resident_cell_range_name = 'Resident' + resident_count.toString();
    var resident_cell = dataSheet.getRange(resident_cell_range_name);
    var resident_text_array = [];
    var resident_bold = [];
    for (var j = 0; j < resident_array.length; j += 3) {
      var last = resident_array[j];
      var first = resident_array[j + 1];
      var number = resident_array[j + 2];
      if (last) {
        var bold_start_stop = [];
        var total = length_all(resident_text_array);
        bold_start_stop.push(total);
        bold_start_stop.push(total + last.length);
        resident_bold.push(bold_start_stop);
        resident_text_array.push(last)
      } 
      if (first) {
        if (number) {
          var name_num = first + ' - ' + number;
          resident_text_array.push(name_num)
        }
        else {
          resident_text_array.push(first)
        }
      } else {
        if (number) {
          resident_text_array.push(number)
        }
      }
    }
    var value = SpreadsheetApp.newRichTextValue();
    var resident_text = 'Some text';
//    var resident_text = resident_text_array.join(String.fromCharCode(10));
//    var resident_text = resident_text_array.join(' ');
    value.setText(resident_text);
//    for (start_stop of resident_bold) {  
//      value.setTextStyle(start_stop[0], start_stop[1], bold);
//    }
    value.build();
//    resident_cell.setValue('text');
    resident_cell.setRichTextValue(value);
    Logger.log(resident_text_array);
    resident_count += 1;
  }
  Logger.log(address_array);
}

function length_all(resident_text_array){
  var total = 0;
    for (each of resident_text_array) {
      total = total + each.length;
    }
  return total;
}
1
Can I ask you about your question? Unfortunately, I cannot understand about As well as: of your goal. Can I ask you about the detail of it? - Tanaike
That is not core to the issue, so I should probably remove my display examples. The error is the problem that I can't overcome. - user1304598
Thank you for replying. If only your this error is removed, please modify resident_cell.setRichTextValue(value) to resident_cell.setRichTextValue(value.build()). In this case, the line of value.build(); can be removed. But I'm not sure whether your script completely works by this modification, because I'm not sure about your actual situation. So I asked about that in my previous comment. I apologize for this. - Tanaike
Thank you! This was the cause of my error. I'm still not sure why, but that did it. - user1304598
Thank you for replying. I'm glad your issue was resolved. When your issue was resolved, can you post it as an answer? By this, it will be also useful for other users who have the same issue. - Tanaike

1 Answers

3
votes

The issue was using the build() command outside of the setRichTextValue() function.

Original that led to the exception:

value.build();
resident_cell.setRichTextValue(value);

Fixed:

resident_cell.setRichTextValue(value.build());

I'm not sure why, but that did it!