【问题标题】:Google Script custom function to return rich text谷歌脚本自定义函数返回富文本
【发布时间】:2019-11-21 18:42:03
【问题描述】:

所以...我已经构建了一个自定义函数,它处理来自 Gsheet 的值并在单个单元格中返回它们。简而言之,源表有一列包含地点,一列包含在这些地点提供的服务,另一列包含结束日期。自定义函数收集在确定日期在同一地点提供的所有服务。它已经在工作并且几乎给了我预期的回报。

返回的字符串:
-> 地点 1:
- 服务 1;
- 服务2。

->地点2:
- 服务 3;
- 服务4。

期望回报:
-> Place1:
- 服务 1;
- 服务2。

->地点2:
- 服务 3;
- 服务4。

我已经搜索过 RichTextValue 的用法,但不知道是因为我使用的是自定义函数的返回还是我不知道如何正确使用它,但不是在职的。这是我用来测试函数的示例代码:

function test(){
  var bold=SpreadsheetApp.newTextStyle().setBold(true).build();
  var test=SpreadsheetApp.newRichTextValue().setText("test").setTextStyle(0,2,bold).build().getText();
  return test;
  }

很明显,有人会说“getText()”返回包含在 RichTextValue 中的字符串,所以它不会返回 RichText,我知道。所以我在“build()”之后尝试了“getRuns().join()”(因为 getRuns 返回数组而加入),我得到了这个结果:“com.google.apps.maestro.server.beans.trix.impl.RichTextValueApiAdapter@ 2d54d6e,com.google.apps.maestro.server.beans.trix.impl.RichTextValueApiAdapter@4c67374a"。

所以我的问题是,是否有一个“getRichText()”作为我的自定义函数的返回或任何其他方式来执行此操作?

提前致谢。

【问题讨论】:

标签: google-apps-script google-sheets rich-text-editor richtext custom-function


【解决方案1】:

实际上不可能在自定义函数上返回格式化数据,只能返回原始值。

您可以使用条件格式或制作自定义菜单或按钮,以便运行构建值及其格式的函数。 像这样的:

// on its startup (Simple trigger) it runs the onOpen function that creates a custom menu and adds a menu item that when clicked runs the function "appendValuestoSheet"
function onOpen(){
  SpreadsheetApp.getUi()
  .createMenu("My Custom Menu")
  .addItem("Calculate Values", "appendValuestoSheet")
  .addToUi();
}

function appendValuestoSheet(){
  var ss = SpreadsheetApp.getActiveSpreadsheet(); // get the current open sheet
  var fontWeights = []; // empty array to be filled with formatting strings (Its size will be based on the newData array size
  var newData = [
      ["Place1", "Service1", "Service2"],
      ["Place1", "Service1", "Service2"]
    ];

  // iterate the new values to add to sheet
  newData.forEach(function(row,rowIndex){
    var newFontWeightsRow, fontWeight;

    // Set bold format only to the first row
    if(rowIndex === 0){
      fontWeight = "bold";
    } else {
      fontWeight = "normal";
    }
    // buids a new row with the formats, in this case only the first row will be bold;
    newFontWeightsRow = row.map(function(){return fontWeight});

    // push the new formats row to the array;
    fontWeights.push(newFontWeightsRow);
  });

   // "fontWeights" array has the same size as "newData" array
  ss.getSheetByName("newDataTab")
    .getRange(1, 1, newData.length, newData[0].length) // adds data starting on the first row and col;
    .setFontWeights(fontWeights) // sets the new formats to the range
    .setValues(newData); // sets the new data to the range
}

【讨论】:

  • 感谢您的回答和令人难以置信的快速回复。还要感谢您提供的解决方法,但在我的情况下,这行不通,我想我只能按原样处理它。
  • 如果有人偶然发现这个问题,请支持这个功能请求:issuetracker.google.com/issues/145501368
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多