You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改Google Sheets脚本的固定范围为可单元格选择的范围?

Fixing the Flexible Bold Cell Counter for Google Sheets

Hey there! I totally get your frustration with the fixed range in that script—let's tweak it so you can use it with any range you want directly in your Google Sheets cells, no more hardcoded ranges. Here's the adjusted function and how to use it:

Modified Script

Replace your original code with this in the Google Apps Script editor:

function COUNTBOLDCELLS(countRange) {
  // Get the range object from the input parameter
  var range = countRange;
  var cellStyles = range.getFontWeights();
  var count = 0;

  // Loop through all cells in the range
  for(var r = 0; r < cellStyles.length; r++) {
    for(var c = 0; c < cellStyles[r].length; c++) {
      if(cellStyles[r][c] === "bold") {
        count++;
      }
    }
  }

  // Return the count so it shows up in the cell where you use the formula
  return count;
}

How to Use This (Super Simple!)

  1. Open your Google Sheet, click Extensions > Apps Script to open the script editor.
  2. Delete all the existing code, paste the modified script above.
  3. Click the save button 💾, and name your project something like "BoldCellCounter" (you can pick any name).
  4. Go back to your sheet, and in any empty cell, type a formula like this:
    =COUNTBOLDCELLS(C3:S3)
    
    Just replace C3:S3 with whatever range you want to count bold cells in—for example, =COUNTBOLDCELLS(D5:Z5) for a different student's row.
  5. The first time you run this, Google will ask for authorization—just follow the prompts to allow the script to access your sheet data (it's safe, you wrote the code!).

What Changed?

  • We renamed the function to COUNTBOLDCELLS (standard convention for spreadsheet functions, makes it easier to remember)
  • Instead of hardcoding a fixed range, we accept a countRange parameter that you pass directly from your sheet
  • We removed the fixed output cell and instead return the count value, so you can use this formula anywhere in your sheet, just like built-in functions like COUNT() or SUM()

This way, you can quickly count bold cells for any student's row by just adjusting the range in the formula—no need to edit the script every time!

内容的提问来源于stack exchange,提问作者Fatema El-Sayed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:44:35