Google Spreadsheets的粗体条件格式化脚本 [英] Bold conditional formatting script for Google Spreadsheets

查看:96
本文介绍了Google Spreadsheets的粗体条件格式化脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的事实质上是这个用户想要在这里做什么:

What I want to do is essentially what this user wanted to do here:


我需要一个脚本来格式化A列中的单元格粗体,但只包含单词'Hello'的单元格。

然而,我完全不知道Google Apps脚本,我需要的答案比我在其他地方或其他地方找到的要简单得多。任何帮助表示赞赏;谢谢!

However I have no knowledge of Google Apps scripts at all, and I need an answer put in much simpler terms than what I could find there or anywhere else. Any help is appreciated; thank you!

推荐答案

首先,从电子表格中打开工具/脚本编辑器...。当对话框打开时,选择为...电子表格创建脚本。您将最终得到一个示例脚本 - 我们将编辑它来做您想做的事。

To start, from your spreadsheet, open "Tools / Script Editor...". When the dialog opens, choose to "Create Script For... Spreadsheet". You will end up with a sample script - we're going to edit it to do what you want.

更改 readRows()函数,如下所示。改变之处在于,不是记录每一行的内容,我们将使用 if if 语句来检查单元格是否包含带有Hello的字符串。然后,如果是这样,我们将加粗单元格文本。

Change the readRows() function as shown here. The change is that instead of logging the content of every row, we will use an if statement to check if the cell contains a string with 'Hello' in it. Then, if it does, we'll bold the cell text.

function readRows() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var rows = sheet.getDataRange();
  var numRows = rows.getNumRows();
  var values = rows.getValues();

  // Arrays start at 0, Google Sheets start at 1 - must remember that.
  // We will loop starting at 1, because we want to skip the header in
  // Row 1, aka Array index 0
  for (var i = 1; i <= numRows - 1; i++) {
    var colA = values[i][0];
    if (colA.toString().indexOf('Hello') >= 0) {
      sheet.getRange(i+1,1).setFontWeight("bold");
    }
  }
};

现在,如何运行?示例中已经有一个 onOpen()函数,它将设置一个自定义菜单...让我们只需更改它在用户界面中显示的文本,如下所示。

Now, how to run that? The sample already has an onOpen() function that will set up a custom menu... let's just change the text it displays in the User Interface, as shown here. The only change is in the 'name' property of the menu entries.

function onOpen() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var entries = [{
    name : "Bold Hello",
    functionName : "readRows"
  }];
  sheet.addMenu("Script Center Menu", entries);
};

保存您的脚本。回到你的电子表格,重新加载它(让它运行onOpen触发器功能)。当你的菜单出现时,你就全部开始设置了。

Save your script. Go back to your spreadsheet, and reload it (to get it to run the onOpen trigger function). When your menu shows up, you're all set.

下一步 - 从第一个脚本教程开始此处。 Google Apps脚本文档涵盖了Apps脚本提供的所有服务,但基本语言结构和对象均为javascript,因此您应该熟悉这一点。试试Google搜索学习javascript,你会发现大量的教程,书籍和其他资源。

Next - start with the "First Script" tutorial here. The Google Apps Script documentation covers all the services provided by Apps Script, but the basic language structure and objects are javascript, so you should get familiar with that. Just try googling "learn javascript", and you'll find tons of tutorials, books, and other resources.

我不能简单一些。

这篇关于Google Spreadsheets的粗体条件格式化脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆