[英]Highlight Cells with Italicized Text
I have created a tab in the menu bar to hold my Schedule function.我在菜单栏中创建了一个选项卡来保存我的计划 function。 This is meant to highlight cells with italicized text to a bright yellow.
这意味着将带有斜体文本的单元格突出显示为亮黄色。 When I run the script below, I get an error reading: Exception: The starting column of the range is too small.
当我运行下面的脚本时,我收到一条错误消息:异常:范围的起始列太小。
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;
}
}
}
Try it this way:试试这种方式:
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;
}
}
}
You have a few mistakes in the way you are building your loop.您在构建循环的方式上存在一些错误。 Have a look at the comments I the following code:
看看我下面代码的评论:
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;
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.