英文:
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);
}
测试:
当运行上述脚本时,将获得以下结果。
注意:
- 此示例脚本是用于理解脚本并将提供的对象转换为电子表格富文本的简单示例脚本。不幸的是,我无法知道您的实际情况。因此,如果您想使用其他样式,请修改上述脚本。
参考资料:
英文:
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.
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:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论