简体   繁体   English

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

[英]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.

相关问题 如果行上的单元格BJ为空,请高亮显示colA中的单元格 - Highlight cell in colA if cells B-J on row are empty 根据先前的单元格值突出显示后续单元格范围 - Highlight a subsequent range of cells based on previous cell value 如果相同的数据存在于另一个范围中,如何突出显示一个范围内的单元格红色 - How to highlight cells red in a range if the same data exists in another range 根据关键字列表突出显示文本中的单词 - Highlight Words in a text, based on a list of keywords 如果变量不同,Google脚本突出显示文本 - Google Script Highlight Text If Variables Differ 使用文本向单元格添加文本 - Apps 脚本 - Adding Text to Cells with Text - Apps Script 突出显示文本并将注释插入特定的Google文档文本 - Highlight text and insert comment into specific Google Doc text 如果单元格通过结合这两个脚本而突出显示为橙色(或任何颜色),则如何“复制”和“粘贴特殊”“仅值” - How to 'copy' and 'paste special' 'values only' if the cells are highlight orange (or any colour) by combining these two scripts 计算具有文本删除线的单元格数量 - Count the number of cells that have the text strikethrough 合并来自不同单元格的文本并保持格式 - Merging text from different cells and keeping the format
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM