MGo*_*eyy 8 text-formatting google-sheets google-apps-script
我正在尝试使用Google Apps脚本中的函数将单元格的格式设置为具有多种字体颜色。我找不到任何文档。另外,使用getFontColor()不会返回任何有用的信息。
有什么办法以编程方式重现此功能
用户可以通过Google表格网络用户界面使用哪些功能?
自 2018 年 7 月起,Apps-Script 支持更改单个文本颜色和其他字体相关样式。两种方法被添加到SpreadsheetApp. newTextStyle()和newRichTextValue()。以下应用程序脚本更改了 A1 中的此类字体样式。为获得最佳效果,请使用较长的字符串(30 个字符或更多)。
function rainbow(){
var rng = SpreadsheetApp.getActiveSheet().getRange("A1");
var val = rng.getValue().toString();
var len = val.length; // length of string in A1
var rich = SpreadsheetApp.newRichTextValue(); //new RichText
rich.setText(val); //Set Text value in A1 to RichText as base
for (var i=0;i<len;i++){ //Loop through each character
var style = SpreadsheetApp.newTextStyle(); // Create a new text style for each character
var red= ("0"+Math.round((1/len)*(i)*255).toString(16)).substr(-2,2); //
var green= ("0"+Math.round((1/len)*Math.min(i*2,len-Math.abs(i*2-len))*255).toString(16)).substr(-2,2); //
var blue= ("0"+Math.round((1/len)*(len-i)*255).toString(16)).substr(-2,2);//
style.setForegroundColor("#"+red+green+blue); // hexcode
style.setFontSize(Math.max(Math.abs(len/2-i),8)); //Use a lengthy string
var buildStyle = style.build();
rich.setTextStyle(i,i+1,buildStyle); // set this text style to the current character and save it to Rich text
}
var format = rich.build()
rng.setRichTextValue(format); //Set the final RichTextValue to A1
}
Run Code Online (Sandbox Code Playgroud)
文档尚未发布。方法可能会发生变化
该表API是有点吓人开始使用,但允许在你的电子表格非常细粒度的控制。您必须启用它,因为它是一项“高级服务”。我强烈建议您查看示例 Codelab。
使用 Sheets API,TextFormatRun可以逐个单元地操作该属性。笔记:
应用于单元格子部分的富文本运行。运行仅对用户输入的字符串有效,对公式、布尔值或数字无效。运行从文本中的特定索引开始,一直持续到下一次运行。除非在后续运行中明确更改,否则运行的属性将继续(除非明确更改,否则第一次运行的属性将继续单元格的属性)。
写入时,新运行将覆盖任何先前运行。写入新的 userEnteredValue 时,之前的运行将被删除。
本示例使用它来调整文本的绿色值,在活动单元格中的字符串长度范围内从 0 增加到 100%。调整以适应您的需求。
function textFormatter() {
// Get the current cell's text.
var wb = SpreadsheetApp.getActive(), sheet = wb.getActiveSheet();
var cell = sheet.getActiveCell(), value = cell.getValue();
var len = value.toString().length;
if(len == 0) return;
// Change the color every 2 characters.
var newCellData = Sheets.newCellData();
newCellData.textFormatRuns = [];
var step = 1 / len;
for(var c = 0; c < len; c += 2) {
var newFmt = Sheets.newTextFormatRun();
newFmt.startIndex = c;
newFmt.format = Sheets.newTextFormat();
newFmt.format.foregroundColor = Sheets.newColor();
newFmt.format.foregroundColor.green = (c + 2) * step;
newCellData.textFormatRuns.push(newFmt);
}
// Create the request object.
var batchUpdateRQ = Sheets.newBatchUpdateSpreadsheetRequest();
batchUpdateRQ.requests = [];
batchUpdateRQ.requests.push(
{
"updateCells": {
"rows": [ { "values": newCellData } ],
"fields": "textFormatRuns",
"start": {
"sheetId": sheet.getSheetId(),
"rowIndex": cell.getRow() - 1,
"columnIndex": cell.getColumn() - 1
}
}
}
);
Sheets.Spreadsheets.batchUpdate(batchUpdateRQ, wb.getId());
}
Run Code Online (Sandbox Code Playgroud)
编辑:根据要设置格式的单元格的值的设置方式,可能还需要在同一请求中包含单元格的值。在问题跟踪器上查看此示例
| 归档时间: |
|
| 查看次数: |
5253 次 |
| 最近记录: |