首页 > 解决方案 > 设置富文本值不适用于具有不同格式的值

问题描述

我正在尝试组合两个富文本字符串,同时保持它们的格式。

function formatCombi() {

    // get the sheet and the page

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();

    // get the plaintext value of both cells, and their combined value

  var text1 = sheet.getRange(1,1).getValue();
  var text2 = sheet.getRange(1,2).getValue();
  var text3 = text1 + text2;

  var range = sheet.getRange(1,3);

    // get the rich text value of both cells

  var rtf1 = sheet.getRange(1,1).getRichTextValue();
  var rtf2 = sheet.getRange(1,2).getRichTextValue();

    // get the length of both strings and the combined length

  var length1 = text1.length;
  var length2 = text2.length;
  var length3 = length1 + length2;

    //get the formats of both strings

  var bold1 = rtf1.getTextStyle().isBold();
  var bold2 = rtf2.getTextStyle().isBold();

  var italic1 = rtf1.getTextStyle().isItalic();
  var italic2 = rtf2.getTextStyle().isItalic();

  var under1 = rtf1.getTextStyle().isUnderline();
  var under2 = rtf2.getTextStyle().isUnderline();

  var strike1 = rtf1.getTextStyle().isStrikethrough();
  var strike2 = rtf2.getTextStyle().isStrikethrough();

  var colour1 = rtf1.getTextStyle().getForegroundColor();
  var colour2 = rtf2.getTextStyle().getForegroundColor();


  //create style for each string

  const style1 = SpreadsheetApp.newTextStyle()
      .setUnderline(under1)
      .setBold(bold1)
      .setItalic(italic1)
      .setForegroundColor(colour1)
      .build();

  const style2 = SpreadsheetApp.newTextStyle()
    .setUnderline(under2)
     .setBold(bold2)
     .setItalic(italic2)
     .setForegroundColor(colour2)
     .build();

  //set the style for string based on the lengths

  var rtf3 = SpreadsheetApp.newRichTextValue()
  .setText(text3)
  .setTextStyle(0,length1,style1)
  .setTextStyle(length1,length3,style2)
  .build();


  range.setRichTextValues(rtf3);

}

这是我到目前为止的代码,但由于某种原因,它总是吐出相同的错误代码:

Exception: The parameters (String) don't match the method signature for SpreadsheetApp.Range.setRichTextValues.
formatCombi @ Code.gs:70

有人知道为什么吗?如果我不能将 RichTextBuilder 设置为一个值,我看不到它能够具有多种格式的意义,并且文档中的示例工作正常。

标签: google-apps-scriptgoogle-sheetsgoogle-sheets-api

解决方案


推荐阅读