是否可以向 Google 表格中附加的行添加格式(阴影)(通过 Google Apps 脚本)
Is it possible to add formatting (shading) to rows being appended in Google Sheets (by Google Apps Script)
我有一个 Google 应用程序脚本,它正在将行从一个 sheet 复制到另一个,执行各种转换。此逻辑最终使用 sheet.appendRow(行详细信息) 将行添加到新的 sheet。我希望这些新创建的行具有背景颜色(我的目的是保留 'latestColour' 以便我可以交替着色)。
那么,是否可以在 appendRow 方法本身中添加底纹,或者轻松确定 appendRow 方法处理的范围,以便我可以应用额外的逻辑来添加底纹。
您可以使用条件格式
=and(A1<>"",A2="")
虽然我不确定我是否能正确理解你的情况,但从你的问题来看,我认为你可能在 Google Spread[=36 中使用了 [格式] --> [交替颜色] =].而且,当通过放置值来附加新行时,您可能希望在附加行中反映“交替颜色”。如果我的猜测是正确的,下面的示例脚本怎么样?
示例脚本:
function myFunction() {
const addValues = ["sample1", "sample2", "sample3"]; // This is a sample appending value. Please replace this for your value.
const sheetName = "Sheet1"; // Please set the sheet name.
// Retrieve banding object from the data range.
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
const b = sheet.getDataRange().getBandings();
if (b.length == 0) {
console.log("Bandings are not used.");
return;
}
// Append the value.
sheet.appendRow(addValues);
// Expand the range of banding.
b[0].setRange(sheet.getDataRange());
}
- 当此脚本为 运行 时,将检索当前条带。并且,在附加值之后,通过包含附加的行来更新条带。在本示例中,即使附加了多行,也可以使用此脚本。
注:
- 根据你的问题,我猜想你的sheet中的数据范围内有一个条带。请注意这一点。
参考文献:
不幸的是,方法 appendRow() 不接收格式设置作为输入,仅接收值数组。
但是,如果您想实现自己的逻辑,这里有一个建议:
示例代码:
function applyColorLastRow() {
var ss = SpreadsheetApp.getActive(); //get active sheets file
var range = ss.getDataRange(); //get populated range, you may want to set a range manually if needed.
var lastRowNum = range.getLastRow(); //getting the last row index of the range.
var lastRowRange = ss.getRange(`${lastRowNum}:${lastRowNum}`); //narrowing the range (using A1 notation) to the last row only to apply color
var lastRowColor = lastRowRange.getCell(1,1).getBackgroundObject().asRgbColor().asHexString();
//Your row coloring logic here...
if (lastRowColor === '#ffffff'){ //toggling white/grey color as an example...
lastRowRange.setBackground('#cccccc'); //apply grey color to all cells in the last row range
} else {
lastRowRange.setBackground('#ffffff'); //apply white color to all cells in the last row range
};
}
我有一个 Google 应用程序脚本,它正在将行从一个 sheet 复制到另一个,执行各种转换。此逻辑最终使用 sheet.appendRow(行详细信息) 将行添加到新的 sheet。我希望这些新创建的行具有背景颜色(我的目的是保留 'latestColour' 以便我可以交替着色)。
那么,是否可以在 appendRow 方法本身中添加底纹,或者轻松确定 appendRow 方法处理的范围,以便我可以应用额外的逻辑来添加底纹。
您可以使用条件格式
=and(A1<>"",A2="")
虽然我不确定我是否能正确理解你的情况,但从你的问题来看,我认为你可能在 Google Spread[=36 中使用了 [格式] --> [交替颜色] =].而且,当通过放置值来附加新行时,您可能希望在附加行中反映“交替颜色”。如果我的猜测是正确的,下面的示例脚本怎么样?
示例脚本:
function myFunction() {
const addValues = ["sample1", "sample2", "sample3"]; // This is a sample appending value. Please replace this for your value.
const sheetName = "Sheet1"; // Please set the sheet name.
// Retrieve banding object from the data range.
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
const b = sheet.getDataRange().getBandings();
if (b.length == 0) {
console.log("Bandings are not used.");
return;
}
// Append the value.
sheet.appendRow(addValues);
// Expand the range of banding.
b[0].setRange(sheet.getDataRange());
}
- 当此脚本为 运行 时,将检索当前条带。并且,在附加值之后,通过包含附加的行来更新条带。在本示例中,即使附加了多行,也可以使用此脚本。
注:
- 根据你的问题,我猜想你的sheet中的数据范围内有一个条带。请注意这一点。
参考文献:
不幸的是,方法 appendRow() 不接收格式设置作为输入,仅接收值数组。
但是,如果您想实现自己的逻辑,这里有一个建议:
示例代码:
function applyColorLastRow() {
var ss = SpreadsheetApp.getActive(); //get active sheets file
var range = ss.getDataRange(); //get populated range, you may want to set a range manually if needed.
var lastRowNum = range.getLastRow(); //getting the last row index of the range.
var lastRowRange = ss.getRange(`${lastRowNum}:${lastRowNum}`); //narrowing the range (using A1 notation) to the last row only to apply color
var lastRowColor = lastRowRange.getCell(1,1).getBackgroundObject().asRgbColor().asHexString();
//Your row coloring logic here...
if (lastRowColor === '#ffffff'){ //toggling white/grey color as an example...
lastRowRange.setBackground('#cccccc'); //apply grey color to all cells in the last row range
} else {
lastRowRange.setBackground('#ffffff'); //apply white color to all cells in the last row range
};
}