如何使用谷歌脚本将多个字体颜色应用到一个单元格 [英] How to use google scripts to apply multiple Font Colors to one Cell
问题描述
我正尝试使用Google脚本中的函数格式化单元格以使其具有多种字体颜色,我无法在其中找到任何文档。同样使用getFontColor()不会返回任何有用的东西。感谢帮助。 示例表
表格API 开始使用有点令人生畏,但是可以对电子表格进行非常细致的控制。您必须启用,因为它是高级服务。我强烈建议您查看示例Codelab 。
使用Sheets API, TextFormatRun
属性可以在逐个单元的基础上进行操作。注意:
应用于单元格子部分的富文本运行。运行只对用户输入的字符串有效,不适用于公式,布尔或数字。从文本中的特定索引开始运行,并继续下一次运行。运行的属性将继续运行,除非在后续运行中明确更改(除非明确更改,否则第一次运行的属性将继续单元的属性)。
在编写时,新的运行将覆盖任何先前的运行。
这个例子使用它来调整文本的绿色值,从0开始增加到活动单元格中字符串长度的100%。调整以适应您的需求。
函数textFormatter(){
//获取当前单元格的文本。
var wb = SpreadsheetApp.getActive(),sheet = wb.getActiveSheet();
var cell = sheet.getActiveCell(),value = cell.getValue();
var len = value.toString()。length;
if(len == 0)return;
//每2个字符更改颜色。
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);
}
//创建请求对象。
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());
}
编辑:要格式化的单元格的值被设置,包括相同请求中的单元格的值也可能是必需的。 查看有关问题跟踪器的示例
I am trying to Format a cell to have multiple Font colors using a function in google scripts, I am unable to find any documentation on it. Also using getFontColor() doesn't return anything useful. Appreciate the help. Example Sheet
The Sheets API is a bit daunting to start using, but allows very fine-grained control over your spreadsheets. You'll have to enable it, as it is an "Advanced Service". I strongly recommend reviewing the Sample Codelab.
With the Sheets API, the TextFormatRun
property can be manipulated on a cell-by-cell basis. Note:
Runs of rich text applied to subsections of the cell. Runs are only valid on user entered strings, not formulas, bools, or numbers. Runs start at specific indexes in the text and continue until the next run. Properties of a run will continue unless explicitly changed in a subsequent run (and properties of the first run will continue the properties of the cell unless explicitly changed).
When writing, the new runs will overwrite any prior runs. When writing a new userEnteredValue, previous runs will be erased.
This example uses it to adjust the green value of text, increasing from 0 to 100% over the length of a string in the active cell. Adjust to suit your needs.
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());
}
Edit: Depending on how the value of the cells to be formatted are set, including the value of the cell in the same request may be necessary as well. See this example on the issue tracker
这篇关于如何使用谷歌脚本将多个字体颜色应用到一个单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!