Bold conditional formatting script for Google Spre

2020-08-09 06:53发布

问题:

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

I need a script that formats the cells in column A bold, but only the cells that contain the word 'Hello'.

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!

回答1:

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.

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");
    }
  }
};

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);
};

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.

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.

I can't make this simpler.



回答2:

In the now not so new 'New' Sheets this can be achieved without a script:

Clear formatting, select ColumnA and Format, Conditional formatting..., Format cells if... Text contains and:

hello

Then for Formatting style click the B and Done.

This way is not case sensitive and will embolden contents such as OTHELLO.



回答3:

If you aren't trying to set too many conditional formatting rules, there's an easier way to set colors, though not bold. In Google Drive Spreadsheet, click the "Format" menu. The bottom menu item should be "Conditional formatting..."; click that. That should produce a dialog box that defaults to something like this (to the extent that I can draw it with text):

                                                                                x
Conditional formatting
[Text contains ◊ ] [                       ]  []Text: [ ]  []Background: [ ]  x
                   e.g. "done" or "Jonathan"
_______________________________________________________________________________

+ Add another rule

[ Save rules ]   [ Cancel ]

In your example, you're looking for cells that contain "Hello", so the default of "Text contains" would do the job. Put "Hello" into the text box, and set a format in the "Text":" and "Background:" boxes. That doesn't give you bold, but it does allow colors.

I see that your question dates back half a year, so it's probably too late for you (and if you strictly need bold, it doesn't solve the problem anyway), but it may help others.