Tag: spreadsheet tricks

  • How to Count or Sum Cells by Color in Google Sheets (with Script)

    🎨 How to Count or Sum Cells Based on Cell Color in Google Sheets

    Google Sheets doesn’t have built-in functions like COUNTBYCOLOR or SUMBYCOLOR, but you can achieve this using Google Apps Script or manual helper columns if using conditional formatting.


    ✅ Method 1: Using Google Apps Script (Best for Manual Cell Colors)

    You can create custom functions to count or sum cells by fill color.

    🔧 Step-by-Step:

    1. Click Extensions > Apps Script
    2. Delete any existing code and paste the following:
    javascriptCopyEdit// Function to count cells with a specific background color
    function countColoredCells(range, color) {
      var sheet = SpreadsheetApp.getActiveSpreadsheet();
      var range = sheet.getRange(range);
      var bgColors = range.getBackgrounds();
      var count = 0;
      
      for (var i = 0; i < bgColors.length; i++) {
        for (var j = 0; j < bgColors[i].length; j++) {
          if (bgColors[i][j] == color) {
            count++;
          }
        }
      }
      return count;
    }
    
    // Function to sum cells with a specific background color
    function sumColoredCells(range, color) {
      var sheet = SpreadsheetApp.getActiveSpreadsheet();
      var range = sheet.getRange(range);
      var bgColors = range.getBackgrounds();
      var values = range.getValues();
      var sum = 0;
      
      for (var i = 0; i < bgColors.length; i++) {
        for (var j = 0; j < bgColors[i].length; j++) {
          if (bgColors[i][j] == color) {
            sum += parseFloat(values[i][j]) || 0;
          }
        }
      }
      return sum;
    }
    
    1. Click the 💾 Save icon and give your project a name.
    2. Back in your sheet, use:
    excelCopyEdit=countColoredCells("A1:A10", "#ffff00")
    =sumColoredCells("A1:A10", "#ffff00")
    

    Replace "A1:A10" with your range and "#ffff00" with the actual color hex code (like Yellow).

    📝 You can find the hex code by:

    • Selecting a colored cell
    • Right-click → View more cell actions > Get cell color hex code

    ⚠️ Notes:

    • This script works only for manually colored cells.
    • It does not detect conditional formatting colors.

    ✅ Bonus: If Using Conditional Formatting

    If color is based on a condition (e.g., values > 100), don’t rely on the color — instead use the same logic:

    excelCopyEdit=COUNTIF(A1:A10, ">100")
    =SUMIF(A1:A10, ">100")
    

    Always use the logic that drives the color rather than the color itself.


    🚀 Want to Learn All These Pro Techniques?

    Everything from custom formulas, conditional formatting, scripts, and data tools is covered in my premium Google Sheets course!

    🔗 📘 Enroll Now – Unlock the Power of Google Sheets

    💥 Limited-Time Offer: ₹1,299 → ₹449 Only!

    🎯 Course Features:

    • 29 detailed video lessons
    • 3h 46m of content from beginner to advanced
    • Covers formulas, charts, pivot tables, automation, Apps Script, and more
    • Ideal for students, professionals, and business users

    💡 Take control of your spreadsheets and become a data ninja with this course!


  • How to Combine Date and Time in Google Sheets (No Add-ons Needed)

    📅⏰ How To Combine Date And Time Columns Into One Column In Google Sheets?

    When working with separate date and time columns, you may need to merge them into a single datetime format. Here’s how to do it effortlessly:

    ✅ Example Setup

    A (Date)B (Time)
    26/06/202510:30 AM

    You want column C to show:
    26/06/2025 10:30 AM


    ✅ Method 1: Use a Simple Formula

    In cell C2, use this formula:

    excelCopyEdit=A2 + B2
    

    What it does:
    In Google Sheets, dates and times are stored as numbers. Adding a date and time simply combines them.


    ✅ Step-by-Step Instructions

    1. Ensure that column A has dates (26/06/2025) and column B has times (10:30 AM).
    2. In column C, enter: =A2+B2
    3. Format column C:
      • Click Format > Number > Custom date and time
      • Use the format: dd/mm/yyyy hh:mm AM/PM or any style you prefer.

    You now have a complete datetime column.


    ⚠️ Common Issues & Fixes

    • Wrong format? → Apply a custom datetime format from the Format menu.
    • #VALUE! error? → Ensure both columns contain valid date and time values.

    🚀 Want to Master Google Sheets from Start to Finish?

    📣 Learn this and hundreds of other practical skills in my complete Google Sheets course:

    🔗 Enroll Now – Unlock the Power of Google Sheets

    💰 Special Offer: ₹1,299 ₹449 (Limited Time Only!)

    🎓 What’s Inside:

    • ✅ 29 value-packed videos
    • ⏱ 3 hours 46 minutes of step-by-step tutorials
    • 📊 Covers formulas, pivot tables, data analysis, automation, and more!
    • 🧠 Easy-to-follow explanations + real-world examples

    👨‍🏫 Whether you’re a student, working professional, or entrepreneur — this course will empower your productivity and data skills.