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
I believe your goal is as follows.
In this case, how about the following 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);
}
When the above script is run, the following result is obtained.