用于返回富文本格式的Google Script自定义函数

问题描述 投票:0回答:1

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

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

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

期望的回报:->地方1:-服务1;-Service2。

->地方2:-服务3;-Service4。

我已经搜索过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个回答
0
投票

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

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

// 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
}

我希望它会有所帮助。最好的问候。

© www.soinside.com 2019 - 2024. All rights reserved.