![](/img/trans.png)
[英]Google Sheets script conditional formatting a cell based on value of that cell
[英]Google Sheets Conditional formatting borders - One cell based on another cell value
我正在嘗試創建腳本,該腳本會根據特定列中單元格的值自動將“藍色”行添加到整行,直到column "BY"
。
當 A 列(示例 A4)中的單元格值與下面的值(示例 A5)不同時,應在單元格 A6 中添加頂部藍色粗線。 此條件格式從第 4 行開始一直到最后。 並且只應應用於電子表格中的特定工作表。 在 Excel 中,我會創建以下條件: =A5<>A4
function myFunction() {
const sheetNames = ["FASHION NL", "FASHION BE","KIDS & UNDERWEAR BNL" ,"NEW BUSINESS BNL" ,"SPORTS & SHOES BNL", "HD&E BNL"] // Please set the sheet names.
const ss = SpreadsheetApp.getActiveSpreadsheet();
sheetNames.forEach(sheetName => {
const sheet = ss.getSheetByName(sheetName);
const range = sheet.getRange("H5:H" + sheet.getLastRow());
const { ranges } = range.getValues().reduce((o, [a], i) => {
if (i == 0) {
o.temp = a;
} else if (i > 0 && o.temp != a) {
o.ranges.push(`A${i + 5}:BY${i + 5}`);
o.temp = a;
}
return o;
}, { ranges: [], temp: "" });
sheet.getRange("A5:BY").setBorder(false, null, false, null, null, false);
sheet.getRangeList(ranges).setBorder(true, null, null, null, null, null, "GREY", SpreadsheetApp.BorderStyle.SOLID_THICK);
});
}
目標是在我的autoSortonEdit()
腳本完成后運行條件格式,並且它應該只影響排序代碼中的活動表。 重要須知: autoSortonEdit()
函數由通過 Google 表格編輯觸發。
現有腳本
function autoSortonEdit(e){
if (e.range.columnStart == 3 && e.range.getValue() != '') {
var sheets = ["FASHION NL", "FASHION BE","KIDS & UNDERWEAR BNL" ,"NEW BUSINESS BNL" ,"SPORTS & SHOES BNL", "HD&E BNL"]; // Please set your expected sheet names.
var sheet = e.range.getSheet();
if (sheets.includes(sheet.getSheetName())) {
var range = sheet.getRange("A5:bY600");
range.sort({ column: 11, ascending: true });
e.source.toast('Sort complete.');
}
}
}
我相信你的目標如下。
在這種情況下,下面的示例腳本怎么樣?
function myFunction() {
const sheetNames = ["Sheet1", "Sheet2",,,]; // Please set the sheet names.
const ss = SpreadsheetApp.getActiveSpreadsheet();
sheetNames.forEach(sheetName => {
const sheet = ss.getSheetByName(sheetName);
const range = sheet.getRange("A5:A" + sheet.getLastRow());
const { ranges } = range.getValues().reduce((o, [a], i) => {
if (i == 0) {
o.temp = a;
} else if (i > 0 && o.temp != a) {
o.ranges.push(`A${i + 5}:BY${i + 5}`);
o.temp = a;
}
return o;
}, { ranges: [], temp: "" });
sheet.getRange("A5:BY").setBorder(false, null, false, null, null, false);
sheet.getRangeList(ranges).setBorder(true, null, null, null, null, null, "blue", SpreadsheetApp.BorderStyle.SOLID);
});
}
在您的示例電子表格圖像中,邊框從“A”列到“C”列。 但是,根據您的問題, I am trying to create script that automatically adds "blue" lines to an entire row until column "BY" based on the value of a cell in a specific column.
,當此腳本運行您的示例電子表格時,相同的邊框將從“A”列到“BY”列。
在這個腳本中,通過sheet.getRange("A5:BY").setBorder(false, false, false, false, false, false);
, "A5:BY" 的初始邊界被清除並放置新邊界。 如果您不想清除現有邊界,請刪除此行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.