用斜体文本突出显示单元格



我在菜单栏中创建了一个选项卡来保存我的日程安排功能。这是为了将带有斜体文本的单元格突出显示为亮黄色。当我运行下面的脚本时,我收到一个错误:例外:范围的起始列太小。

function onOpen() {
var ui = SpreadsheetApp.getUi();
var menu = ui.createMenu("Scheduling");
menu.addItem("Schedule","schedule");
menu.addToUi();
}
function schedule() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("CANVAS");
var col = ss.getLastColumn();
var row = ss.getLastRow();
for (var i = 1; i <= col; i++) {
for (var j = 1; j <= row; i++) {
if (ss.getRange(i,j).getFontStyle() == "italic") {
ss.getRange(i,j).setBackground("#fff2cc");
j = j+1;
} else {
j = j+1;
}
i = i+1;
j = 0;
}
}
}

试试这个方式:

function schedule() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("CANVAS");
var col = ss.getLastColumn();
var row = ss.getLastRow();
//reversed row and col end points from your code
for (var i = 1; i <= row; i++) {
for (var j = 1; j <= col; i++) {
if (ss.getRange(i,j).getFontStyle() == "italic") {
ss.getRange(i,j).setBackground("#fff2cc");
j = j+1;
} else {
j = j+1;
}
i = i+1;
j = 0;
}
}
}

你在构建循环的方式上有一些错误。 看看下面的代码的注释:

function schedule() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("CANVAS");
var col = ss.getLastColumn();
var row = ss.getLastRow();
// For each column
for (var i = 1; i <= col; i++) {
// Loop through the rows
for (var j = 1; j <= row; j++) {  // Error! i++ should be j++.
// Check font-style
if (ss.getRange(i,j).getFontStyle() == "italic") {
// If italic, color the background
ss.getRange(i,j).setBackground("#fff2cc"); 
/* The following makes no sense here... If all you are verifying 
is the italic condition, then let the loop finish and j will be incremented */
// j = j+1;
} 
}
}
}

相关内容

  • 没有找到相关文章

最新更新