ไปยังเนื้อหาหลัก

วิธีการนับหรือรวมเซลล์ตามสีของเซลล์ใน Google ชีต

การนับหรือรวมค่าเซลล์ตามสีพื้นหลังของเซลล์เฉพาะเพื่อให้ได้ผลลัพธ์ตามภาพหน้าจอต่อไปนี้ บทความนี้จะพูดถึงวิธีแก้งานนี้ใน Google sheet และ Microsoft Excel

นับค่าเซลล์ตามสีของเซลล์ด้วยสคริปต์ใน Google ชีต

รวมค่าเซลล์ตามสีของเซลล์ด้วยสคริปต์ใน Google ชีต

นับหรือรวมค่าเซลล์บนสีของเซลล์ด้วย Kutools for Excel ใน Microsoft Excel


นับค่าเซลล์ตามสีของเซลล์ด้วยสคริปต์ใน Google ชีต

สคริปต์ต่อไปนี้สามารถช่วยคุณในการนับค่าของเซลล์ตามสีของเซลล์ที่ระบุได้โปรดทำดังนี้:

1. คลิก เครื่องมือ > โปรแกรมแก้ไขสคริปต์ดูภาพหน้าจอ:

2. ในหน้าต่างโปรเจ็กต์ที่เปิดอยู่ให้คลิก เนื้อไม่มีมัน > ใหม่ > ไฟล์สคริปต์ เพื่อเปิดหน้าต่างรหัสดูภาพหน้าจอ:

3. และในกล่องพร้อมต์โปรดป้อนชื่อสำหรับรหัสสคริปต์นี้ดูภาพหน้าจอ:

4. คลิก OK จากนั้นคัดลอกและวางรหัสต่อไปนี้เพื่อแทนที่รหัสเดิมลงในโมดูลรหัสดูภาพหน้าจอ:

function countColoredCells(countRange,colorRef) {
  var activeRg = SpreadsheetApp.getActiveRange();
  var activeSht = SpreadsheetApp.getActiveSheet();
  var activeformula = activeRg.getFormula();
  var countRangeAddress = activeformula.match(/\((.*)\,/).pop().trim();
  var backGrounds = activeSht.getRange(countRangeAddress).getBackgrounds();
  var colorRefAddress = activeformula.match(/\,(.*)\)/).pop().trim();
  var BackGround = activeSht.getRange(colorRefAddress).getBackground();
  var countCells = 0;
  for (var i = 0; i < backGrounds.length; i++)
    for (var k = 0; k < backGrounds[i].length; k++)
      if ( backGrounds[i][k] == BackGround )
        countCells = countCells + 1;
  return countCells;
};

5. จากนั้นบันทึกรหัสสคริปต์นี้และกลับไปที่แผ่นงานป้อนสูตรนี้: = เซลล์นับสี (A1: E11, A1) ลงในเซลล์ว่างจากนั้นกด เข้าสู่ คีย์เพื่อรับผลลัพธ์จากการคำนวณ ดูภาพหน้าจอ:

หมายเหตุ: ในสูตรนี้: A1: E11 คือช่วงข้อมูลที่คุณต้องการใช้ A1 คือเซลล์ที่เต็มไปด้วยสีเฉพาะที่คุณต้องการนับ

6. ทำซ้ำสูตรด้านบนเพื่อนับเซลล์สีอื่น ๆ


รวมค่าเซลล์ตามสีของเซลล์ด้วยสคริปต์ใน Google ชีต

หากต้องการรวมค่าเซลล์ด้วยสีของเซลล์เฉพาะโปรดใช้รหัสสคริปต์ด้านล่าง

1. คลิก เครื่องมือ > โปรแกรมแก้ไขสคริปต์ ไปที่หน้าต่างโครงการแล้วคลิก เนื้อไม่มีมัน > ใหม่ > ไฟล์สคริปต์ เพื่อแทรกโมดูลรหัสใหม่จากนั้นในกล่องพร้อมต์โปรดพิมพ์ชื่อสำหรับสคริปต์นี้ดูภาพหน้าจอ:

2. คลิก OK และในโมดูลโค้ดที่เปิดให้คัดลอกและวางโค้ดสคริปต์ด้านล่างเพื่อแทนที่โค้ดต้นฉบับดูภาพหน้าจอ:

function sumColoredCells(sumRange,colorRef) {
  var activeRg = SpreadsheetApp.getActiveRange();
  var activeSht = SpreadsheetApp.getActiveSheet();
  var activeformula = activeRg.getFormula();
  var countRangeAddress = activeformula.match(/\((.*)\,/).pop().trim();
  var backGrounds = activeSht.getRange(countRangeAddress).getBackgrounds();
  var sumValues = activeSht.getRange(countRangeAddress).getValues();  
  var colorRefAddress = activeformula.match(/\,(.*)\)/).pop().trim();
  var BackGround = activeSht.getRange(colorRefAddress).getBackground();
  var totalValue = 0;
  for (var i = 0; i < backGrounds.length; i++)
    for (var k = 0; k < backGrounds[i].length; k++)
      if ( backGrounds[i][k] == BackGround )
        if ((typeof sumValues[i][k]) == 'number')
          totalValue = totalValue + (sumValues[i][k]);
  return totalValue;
};

3. จากนั้นบันทึกรหัสนี้กลับไปที่แผ่นงานและป้อนสูตรนี้: = sumcoloredcells (A1: E11, A1) ลงในเซลล์ว่างแล้วกด เข้าสู่ คีย์เพื่อรับผลลัพธ์จากการคำนวณดูภาพหน้าจอ:

หมายเหตุ: ในสูตรนี้: A1: E11 คือช่วงข้อมูลที่คุณต้องการใช้ A1 คือเซลล์ที่มีสีพื้นหลังเฉพาะที่คุณต้องการรวม

4. จากนั้นคุณสามารถทำซ้ำสูตรด้านบนเพื่อรวมเซลล์สีอื่น ๆ


นับหรือรวมค่าเซลล์บนสีของเซลล์ด้วย Kutools for Excel ใน Microsoft Excel

ใน Microsoft Excel เพื่อนับหรือรวมค่าของเซลล์ตามสีของเซลล์ที่ระบุ Kutools สำหรับ Excel's นับตามสี ยูทิลิตี้สามารถช่วยให้คุณทำงานนี้ให้เสร็จโดยเร็วที่สุด

Kutools สำหรับ Excel : ด้วย Add-in ของ Excel ที่มีประโยชน์มากกว่า 300 รายการทดลองใช้ฟรีโดยไม่มีข้อ จำกัด ใน 30 วัน. 

หลังจากการติดตั้ง Kutools สำหรับ Excelโปรดทำตามนี้:

1. เลือกเซลล์ในช่วงที่คุณต้องการนับหรือผลรวมตามสีของเซลล์จากนั้นคลิก Kutools พลัส > นับตามสีดูภาพหน้าจอ:

2. ใน นับตามสี ให้เลือก การจัดรูปแบบมาตรฐาน จาก วิธีการสี รายการแบบหล่นลงจากนั้นเลือก พื้นหลัง จาก ประเภทการนับ แบบเลื่อนลงดูภาพหน้าจอ:

3. จากนั้นคลิก สร้างรายงาน ปุ่มและแผ่นงานใหม่ที่มีผลลัพธ์จากการคำนวณจะถูกสร้างขึ้นพร้อมกันดูภาพหน้าจอ:

หมายเหตุ: ด้วยคุณสมบัติที่มีประสิทธิภาพนี้คุณยังสามารถคำนวณค่าของเซลล์ตามการจัดรูปแบบตามเงื่อนไขหรือสีแบบอักษร

คลิกดาวน์โหลดและทดลองใช้ฟรี Kutools for Excel ทันที!

สุดยอดเครื่องมือเพิ่มผลผลิตในสำนักงาน

🤖 Kutools AI ผู้ช่วย: ปฏิวัติการวิเคราะห์ข้อมูลโดยยึดตาม: การดำเนินการที่ชาญฉลาด   |  สร้างรหัส  |  สร้างสูตรที่กำหนดเอง  |  วิเคราะห์ข้อมูลและสร้างแผนภูมิ  |  เรียกใช้ฟังก์ชัน Kutools...
คุณสมบัติยอดนิยม: ค้นหา เน้น หรือระบุรายการที่ซ้ำกัน   |  ลบแถวว่าง   |  รวมคอลัมน์หรือเซลล์โดยไม่สูญเสียข้อมูล   |   รอบโดยไม่มีสูตร ...
การค้นหาขั้นสูง: VLookup หลายเกณฑ์    VLookup หลายค่า  |   VLookup ข้ามหลายแผ่น   |   การค้นหาที่ไม่ชัดเจน ....
รายการแบบเลื่อนลงขั้นสูง: สร้างรายการแบบหล่นลงอย่างรวดเร็ว   |  รายการแบบหล่นลงขึ้นอยู่กับ   |  เลือกหลายรายการแบบหล่นลง ....
ผู้จัดการคอลัมน์: เพิ่มจำนวนคอลัมน์เฉพาะ  |  ย้ายคอลัมน์  |  สลับสถานะการมองเห็นของคอลัมน์ที่ซ่อนอยู่  |  เปรียบเทียบช่วงและคอลัมน์ ...
คุณสมบัติเด่น: กริดโฟกัส   |  มุมมองการออกแบบ   |   บาร์สูตรใหญ่    สมุดงานและตัวจัดการชีต   |  ห้องสมุดทรัพยากร (ข้อความอัตโนมัติ)   |  เลือกวันที่   |  รวมแผ่นงาน   |  เข้ารหัส/ถอดรหัสเซลล์    ส่งอีเมลตามรายการ   |  ซุปเปอร์ฟิลเตอร์   |   ตัวกรองพิเศษ (กรองตัวหนา/ตัวเอียง/ขีดทับ...) ...
ชุดเครื่องมือ 15 อันดับแรก12 ข้อความ เครื่องมือ (เพิ่มข้อความ, ลบอักขระ, ... )   |   50 + แผนภูมิ ประเภท (แผนภูมิ Gantt, ... )   |   40+ ใช้งานได้จริง สูตร (คำนวณอายุตามวันเกิด, ... )   |   19 การแทรก เครื่องมือ (ใส่ QR Code, แทรกรูปภาพจากเส้นทาง, ... )   |   12 การแปลง เครื่องมือ (ตัวเลขเป็นคำ, การแปลงสกุลเงิน, ... )   |   7 ผสานและแยก เครื่องมือ (แถวรวมขั้นสูง, แยกเซลล์, ... )   |   ... และอื่น ๆ

เพิ่มพูนทักษะ Excel ของคุณด้วย Kutools สำหรับ Excel และสัมผัสประสิทธิภาพอย่างที่ไม่เคยมีมาก่อน Kutools สำหรับ Excel เสนอคุณสมบัติขั้นสูงมากกว่า 300 รายการเพื่อเพิ่มประสิทธิภาพและประหยัดเวลา  คลิกที่นี่เพื่อรับคุณสมบัติที่คุณต้องการมากที่สุด...

รายละเอียด


แท็บ Office นำอินเทอร์เฟซแบบแท็บมาที่ Office และทำให้งานของคุณง่ายขึ้นมาก

  • เปิดใช้งานการแก้ไขและอ่านแบบแท็บใน Word, Excel, PowerPoint, ผู้จัดพิมพ์, Access, Visio และโครงการ
  • เปิดและสร้างเอกสารหลายรายการในแท็บใหม่ของหน้าต่างเดียวกันแทนที่จะเป็นในหน้าต่างใหม่
  • เพิ่มประสิทธิภาพการทำงานของคุณ 50% และลดการคลิกเมาส์หลายร้อยครั้งให้คุณทุกวัน!
Comments (79)
Rated 5 out of 5 · 1 ratings
This comment was minimized by the moderator on the site
Hey I just wanted to thank you for this guide. It totally worked! Awesome! 👍
This comment was minimized by the moderator on the site
Hii, i need help!
Did somebody have the right script?
if u have it, can u pls send me?
thanks
This comment was minimized by the moderator on the site
i need the auto update for the sumcoloredcells formula
do you know how can i have it?
This comment was minimized by the moderator on the site
questo funziona

// restituisce la somma dei valori in un range in base al colore di sfondo della cella
// uso: =SommaSeSfondo(range;cella colore di riferimento )
// =SommaSeSfondo(B1:B8;E1)

function SommaSeSfondo(sumRange,colorRef) {
var activeRg = SpreadsheetApp.getActiveRange();
var activeSht = SpreadsheetApp.getActiveSheet();
var activeformula = activeRg.getFormula();
var countRangeAddress = activeformula.match(/\((.*)\;/).pop().trim();
var backGrounds = activeSht.getRange(countRangeAddress).getBackgrounds();
var sumValues = activeSht.getRange(countRangeAddress).getValues();
var colorRefAddress = activeformula.match(/\;(.*)\)/).pop().trim();
var BackGround = activeSht.getRange(colorRefAddress).getBackground();
var totalValue = 0;
for (var i = 0; i < backGrounds.length; i++)
for (var k = 0; k < backGrounds[i].length; k++)
if ( backGrounds[i][k] == BackGround )
if ((typeof sumValues[i][k]) == 'number')
totalValue = totalValue + (sumValues[i][k]);
return totalValue;
};
This comment was minimized by the moderator on the site
Hi Samal,

You have to use the formula:

=SommeCouleurs(A:A;A1) => A:A : columns that contain the colors to count and A1 : The cell that contains the example of the color.
This comment was minimized by the moderator on the site
'm having a problem in the script for google sheets: TypeError: Cannot call method "pop" of null. (line 5, file "SumByColor"). Can you please help?
This comment was minimized by the moderator on the site
'm having a problem in the script for google sheets: TypeError: Cannot call method "pop" of null. (line 5, file "SumByColor"). Can you please help?
This comment was minimized by the moderator on the site
'm having a problem in the script for google sheets: TypeError: Cannot call method "pop" of null. (line 5, file "SumByColor"). Can you please help?
This comment was minimized by the moderator on the site
Bonjour a tous,

J'utilise Google Sheet en version Française et voici le script le plus fonctionnel que j'ai trouvé:

function SommeCouleurs(plage,couleur) {
var activeRange = SpreadsheetApp.getActiveRange();
var activeSheet = activeRange.getSheet();
var formule = activeRange.getFormula();


var laplage = formule.match(/\((.*)\;/).pop();
var range = activeSheet.getRange(laplage);
var bg = range.getBackgrounds();
var values = range.getValues();

var lacouleur = formule.match(/\;(.*)\)/).pop();
var colorCell = activeSheet.getRange(lacouleur);
var color = colorCell.getBackground();

var total = 0;

for(var i=0;i<bg.length;i++)
for(var j=0;j<bg[0].length;j++)
if( bg[i][j] == color )
total=total+(values[i][j]*1);
return total;

};


function CompteCouleurs(plage,couleur) {
var activeRange = SpreadsheetApp.getActiveRange();
var activeSheet = activeRange.getSheet();
var formule = activeRange.getFormula();


var laplage = formule.match(/\((.*)\;/).pop();
var range = activeSheet.getRange(laplage);
var bg = range.getBackgrounds();
var values = range.getValues();

var lacouleur = formule.match(/\;(.*)\)/).pop();
var colorCell = activeSheet.getRange(lacouleur);
var color = colorCell.getBackground();

var count = 0;


for(var i=0;i<bg.length;i++)
for(var j=0;j<bg[0].length;j++)
if( bg[i][j] == color )
count=count+1;
return count;
};

Formule a utiliser:

=CompteCouleurs(A2:A;A1)

Il s'actualise automatiquement.
En espérant que ca vous aidera
This comment was minimized by the moderator on the site
Добрый день!
Проверил, автоматически не пересчитывает, если изменить цвет в диапазоне ((
This comment was minimized by the moderator on the site
非常感謝大大分享~很實用我是用Google 試算表編輯
請問程式碼能教怎麼加總指定的文字顏色的數字總和嗎? (把 儲存格顏色 改為 文字顏色) 拜託~~
This comment was minimized by the moderator on the site
Hi, I have set it in google sheets as You have described, but it showed me error "Type error: Cannot read property 'pop' of null". PLS can you help me?
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations