我写了两个函数。第一个以富文本形式获取单元格的内容,并将文本和属性作为单独的运行存储在对象数组中。每次运行都有一个对象。 getRichTextValue 记录在这里https://developers.google.com/apps-script/reference/spreadsheet/rich-text-value
function getRichTextCell(fdbkRgA1) {
console.log('Begin getRichTextCell input fdbkRgA1: ', fdbkRgA1 );
const ss = SpreadsheetApp.getActiveSpreadsheet()
const sheet = ss.getActiveSheet();
const inRng = sheet.getRange(fdbkRgA1);
let richText = ss.getRange(fdbkRgA1).getRichTextValue();
let thisText = richText.getText();
// console.log('Cell content: ' + thisText);
let runsArr = richText.getRuns();
// console.log('runs length: ' + runsArr.length );
let rtObjArr = [];
for ( i = 0 ; i < runsArr.length ; i++ ) {
// get the style of this section of text, between startIndex and endIndex
let run = runsArr[i];
let runtext = run.getText();
let color = run.getTextStyle().getForegroundColor();
let font = run.getTextStyle().getFontFamily();
let fontsize = run.getTextStyle().getFontSize();
let startIndex = run.getStartIndex();
let endIndex = run.getEndIndex();
let styleBold = run.getTextStyle().isBold();
let styleItalic = run.getTextStyle().isItalic();
let stylesThru = run.getTextStyle().isStrikethrough();
let styleUline = run.getTextStyle().isUnderline();
// console.log('i: ', i, ' Start Idx: ', startIndex,
// ' End Idx: ', endIndex,
// ' Text: ', runtext,
// ' Bold?: ', styleBold,
// ' Color: ', color,
// ' Font: ', font,
// ' Size: ', fontsize,
// ' Italic?: ', styleItalic,
// ' Strikethru?: ', stylesThru,
// ' Underline?: ', styleUline);
rtObjArr.push ({
'StartIdx' : startIndex,
'EndIdx' : endIndex,
'Text' : runtext,
'Bold' : styleBold,
'Color' : color,
'Font' : font,
'Size' : fontsize,
'Italic' : styleItalic,
'Strikethru' : stylesThru,
'Underline' : styleUline
});
}
return rtObjArr;
}
第二个函数接收带有富文本和属性运行的对象,并将其添加到现有文档的顶部或底部。
function setRichTextDoc(rtObjArr, stuDoc, TorB, docTit) {
console.log('Begin setRichTextDoc input docTit: ', docTit);
let numRuns = rtObjArr.length;
// console.log('numRuns: ', numRuns);
if ( numRuns === 0 ) {
console.log('No feedback found for student doc.' + docTit +
'\nSKIPPING' );
ui.alert('No feedback found for student doc.' + docTit +
'\nSKIPPING');
return;
}
let theTxt, runtext, color, font, fontsize, para,
styleBold, styleItalic, stylesThru, styleUline;
if ( TorB === 'BOTTOM' ) {
// console.log('begin before stuff BOTTOM');
stuDoc.appendHorizontalRule();
para = stuDoc.appendParagraph('Feedback on Current Draft');
para.setForegroundColor('#000000')
.setAlignment(DocumentApp.HorizontalAlignment.CENTER)
.setBold(true);
para = stuDoc.appendParagraph("")
} else {
// console.log('begin before stuff TOP');
para = stuDoc.insertParagraph(0, "")
stuDoc.insertHorizontalRule(1);
para = stuDoc.insertParagraph(2, 'Final Comments');
para.setForegroundColor('#000000')
.setAlignment(DocumentApp.HorizontalAlignment.CENTER)
.setBold(true);
para = stuDoc.insertParagraph(3, "")
}
for ( i = 0 ; i < numRuns ; i++ ) {
console.log ('i: ', i, ' stringify input object\n', JSON.stringify(rtObjArr[i]) );
runtext = rtObjArr[i].Text;
color = rtObjArr[i].Color;
font = rtObjArr[i].Font;
fontsize = rtObjArr[i].Size;
styleBold = rtObjArr[i].Bold;
styleItalic = rtObjArr[i].Italic;
stylesThru = rtObjArr[i].Strikethru;
styleUline = rtObjArr[i].Underline;
if ( TorB === 'BOTTOM' ) {
// console.log('begin rich text BOTTOM');
theTxt = para.appendText(runtext); // Exception: Cannot insert an empty text element.
// theTxt = stuDoc.appendText(runtext); // Err: TypeError: stuDoc.appendText is not a function
// theTxt = stuDoc.appendParagraph(runtext); // runs but nothing stored
theTxt.setFontFamily(font)
.setFontSize(fontsize)
.setForegroundColor(color)
.setBold(styleBold)
.setItalic(styleItalic)
.setStrikethrough(stylesThru)
.setUnderline(styleUline);
} else {
// console.log('begin rich text TOP');
theTxt = para.appendText(runtext);
theTxt.setFontFamily(font)
.setFontSize(fontsize)
.setForegroundColor(color)
.setBold(styleBold)
.setItalic(styleItalic)
.setStrikethrough(stylesThru)
.setUnderline(styleUline);
}
// console.log ('i: ', i, ' finished' );
}
if ( TorB === 'BOTTOM' ) {
console.log('begin after stuff BOTTOM');
para = stuDoc.appendParagraph('Create Your Next Draft Below');
para.setAlignment(DocumentApp.HorizontalAlignment.CENTER)
.setBold(true);
stuDoc.appendHorizontalRule();
} else {
console.log('begin after stuff TOP');
stuDoc.insertHorizontalRule(4);
stuDoc.insertParagraph(5, '');
}
console.log('End setRichTextDoc');
}
代码保留新行和左水平对齐,即使这些不是可用的属性。
如果没有@Chris Google Docs Script: appendText/insertText Formatting 的帮助,我无法解决这个问题@