Google Apps Script:从API读取RTF值并将其粘贴为格式化文本到电子表格中

huangapple go评论71阅读模式
英文:

Google Apps Script: Read rtf value from API and paste as formatted text into Spreadsheet

问题

我正在使用Google Apps Script来提取以下形式的富文本:

{"blocks":[{"key":"dgn8a","text":"This Text is rich.","type":"unstyled","depth":0,"inlineStyleRanges":[{"offset":0,"length":4,"style":"ITALIC"},{"offset":13,"length":4,"style":"ITALIC"},{"offset":0,"length":4,"style":"BOLD"},{"offset":5,"length":4,"style":"UNDERLINE"}],"entityRanges":[],"data":{}}],"entityMap":{}}

我想要将这段文本以正确的格式粘贴到Google表格中。

英文:

I am using Google Apps Script to fetch rich text in the following form:

{"blocks":[{"key":"dgn8a","text":"This Text is rich.","type":"unstyled","depth":0,"inlineStyleRanges":[{"offset":0,"length":4,"style":"ITALIC"},{"offset":13,"length":4,"style":"ITALIC"},{"offset":0,"length":4,"style":"BOLD"},{"offset":5,"length":4,"style":"UNDERLINE"}],"entityRanges":[],"data":{}}],"entityMap":{}}

and I would like to paste this text in the correct format to a Google sheet.

Can anybody help?

Thanks in advance.

Regards
Lars

答案1

得分: 1

  • 你有已转换为富文本的元数据对象,使用 draft-js
  • 你想将这个对象放入电子表格的单元格中作为富文本。

在这种情况下,可以考虑以下示例脚本:

function myFunction() {
  // This is from your question.
  const obj = { "blocks": [{ "key": "dgn8a", "text": "This Text is rich.", "type": "unstyled", "depth": 0, "inlineStyleRanges": [{ "offset": 0, "length": 4, "style": "ITALIC" }, { "offset": 13, "length": 4, "style": "ITALIC" }, { "offset": 0, "length": 4, "style": "BOLD" }, { "offset": 5, "length": 4, "style": "UNDERLINE" }], "entityRanges": [], "data": {} }], "entityMap": {} };

  // Convert from your object to rich text for a Spreadsheet.
  const convStyles = { "BOLD": "setBold", "ITALIC": "setItalic", "UNDERLINE": "setUnderline" };
  const richTextValues = obj.blocks.map(({ inlineStyleRanges, text }) => {
    const temp = SpreadsheetApp.newRichTextValue().setText(text);
    inlineStyleRanges.forEach(({ offset, length, style }) =>
      temp.setTextStyle(offset, offset + length, SpreadsheetApp.newTextStyle()[convStyles[style]](true).build())
    );
    return [temp.build()];
  });

  // Put rich text to the cells.
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1"); // 请设置您的工作表名称。
  sheet.getRange(1, 1, richTextValues.length).setRichTextValues(richTextValues);
}

测试:
当运行上述脚本时,将获得以下结果。

Google Apps Script:从API读取RTF值并将其粘贴为格式化文本到电子表格中

注意:

  • 此示例脚本是用于理解脚本并将提供的对象转换为电子表格富文本的简单示例脚本。不幸的是,我无法知道您的实际情况。因此,如果您想使用其他样式,请修改上述脚本。

参考资料:

英文:

I believe your goal is as follows.

  • You have the metadata object of converted rich texts with draft-js.
  • You want to put your object to cells of the Spreadsheet as the rich text.

In this case, how about the following sample script?

Sample script:

function myFunction() {
  // This is from your question.
  const obj = { "blocks": [{ "key": "dgn8a", "text": "This Text is rich.", "type": "unstyled", "depth": 0, "inlineStyleRanges": [{ "offset": 0, "length": 4, "style": "ITALIC" }, { "offset": 13, "length": 4, "style": "ITALIC" }, { "offset": 0, "length": 4, "style": "BOLD" }, { "offset": 5, "length": 4, "style": "UNDERLINE" }], "entityRanges": [], "data": {} }], "entityMap": {} };

  // Convert from your object to rich text for a Spreadsheet.
  const convStyles = { "BOLD": "setBold", "ITALIC": "setItalic", "UNDERLINE": "setUnderline" };
  const richTextValues = obj.blocks.map(({ inlineStyleRanges, text }) => {
    const temp = SpreadsheetApp.newRichTextValue().setText(text);
    inlineStyleRanges.forEach(({ offset, length, style }) =>
      temp.setTextStyle(offset, offset + length, SpreadsheetApp.newTextStyle()[convStyles[style]](true).build())
    );
    return [temp.build()];
  });

  // Put rich text to the cells.
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1"); // Please set your sheet name.
  sheet.getRange(1, 1, richTextValues.length).setRichTextValues(richTextValues);
}

Testing:

When the above script is run, the following result is obtained.

Google Apps Script:从API读取RTF值并将其粘贴为格式化文本到电子表格中

Note:

  • This sample script is a simple sample script for understanding the script and converting your provided object to rich text for the Spreadsheet. Unfortunately, I cannot know your actual situation. So, when you want to use other styles, please modify the above script.

References:

huangapple
  • 本文由 发表于 2023年6月26日 21:15:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76557048.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定