如何修改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!)
- Open your Google Sheet, click Extensions > Apps Script to open the script editor.
- Delete all the existing code, paste the modified script above.
- Click the save button 💾, and name your project something like "BoldCellCounter" (you can pick any name).
- Go back to your sheet, and in any empty cell, type a formula like this:
Just replace=COUNTBOLDCELLS(C3:S3)C3:S3with whatever range you want to count bold cells in—for example,=COUNTBOLDCELLS(D5:Z5)for a different student's row. - 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
countRangeparameter 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()orSUM()
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
相关产品推荐
相关产品推荐

