คู่มือเริ่มใช้งานคลังอย่างรวดเร็ว
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
สร้างคลัง Apps Script ที่คุณใช้เพื่อนำแถวที่ซ้ำกันในข้อมูลสเปรดชีตออกได้
วัตถุประสงค์
- ตั้งค่าสคริปต์
- เรียกใช้สคริปต์
ข้อกำหนดเบื้องต้น
หากต้องการใช้ตัวอย่างนี้ คุณต้องมีข้อกำหนดเบื้องต้นต่อไปนี้
- บัญชี Google (บัญชี Google Workspace อาจต้อง
ได้รับการอนุมัติจากผู้ดูแลระบบ)
- เว็บเบราว์เซอร์ที่มีสิทธิ์เข้าถึงอินเทอร์เน็ต
ตั้งค่าสคริปต์
หากต้องการสร้างคลัง ให้ทำตามขั้นตอนต่อไปนี้
- ลงชื่อเข้าใช้บัญชี Google
- หากต้องการเปิดเครื่องมือแก้ไขสคริปต์ ให้ไปที่ script.google.com
- คลิกโปรเจ็กต์ใหม่ที่ด้านซ้ายบน
ลบโค้ดในตัวแก้ไขสคริปต์ แล้ววางโค้ดด้านล่าง
คลิกบันทึก 
คลิกโปรเจ็กต์ที่ไม่มีชื่อที่ด้านซ้ายบน
ตั้งชื่อสคริปต์ว่า Remove duplicate rows แล้วคลิก Rename
คลิกทำให้ใช้งานได้ > การทำให้ใช้งานได้ใหม่
คลิกเปิดใช้ประเภทการติดตั้งใช้งาน
> คลัง ข้างเลือกประเภท
ป้อนคำอธิบายของไลบรารี เช่น นำแถวที่ซ้ำกันออก ทุกคน
ที่มีสิทธิ์เข้าถึงคลังจะดูคำอธิบายนี้ได้
คลิกทำให้ใช้งานได้
คลิกการตั้งค่าโปรเจ็กต์
ทางด้านซ้าย
ในส่วนรหัส ให้คัดลอกรหัสสคริปต์เพื่อใช้ในขั้นตอนถัดไป
เรียกใช้สคริปต์
หากต้องการใช้ไลบรารี คุณต้องมีสิทธิ์ดูโปรเจ็กต์ Apps Script ของไลบรารีนั้นเป็นอย่างน้อย เนื่องจากคุณเป็นผู้สร้างไลบรารี คุณจึงมี
สิทธิ์ที่จำเป็น
ในการใช้งาน หากต้องการอนุญาตให้ผู้อื่นใช้ไลบรารี ให้สิทธิ์ดู
สำหรับโปรเจ็กต์ Apps Script แก่บุคคลดังกล่าว
หากต้องการใช้คลัง ให้ทำตามขั้นตอนต่อไปนี้
- เปิดสเปรดชีต Google ชีตที่มีข้อมูลซึ่งมีแถวที่ซ้ำกัน หากต้องการใช้สเปรดชีตตัวอย่าง ให้ทำสำเนาสเปรดชีตตัวอย่างแถวที่ซ้ำกัน
- คลิกส่วนขยาย > Apps Script
- คลิกเพิ่มคลัง
addข้างคลัง
- ในส่วนรหัสสคริปต์ ให้วางรหัสสคริปต์จากโปรเจ็กต์ Apps Script ของไลบรารี
ที่คุณคัดลอกไว้ในส่วนก่อนหน้า
- คลิกค้นหา
- เลือก 1 ในส่วนเวอร์ชัน
- คลิกเพิ่ม
ลบโค้ดในตัวแก้ไขสคริปต์ แล้ววางโค้ดด้านล่าง
function runLibrary() {
Removeduplicaterows.removeDuplicates();
}
ในเมนูแบบเลื่อนลงของฟังก์ชัน ให้เลือก runLibrary
คลิกเรียกใช้
กลับไปที่สเปรดชีตเพื่อดูข้อมูลที่อัปเดตแล้วโดยไม่มีแถวที่ซ้ำกัน
ตรวจสอบโค้ด
หากต้องการตรวจสอบโค้ด Apps Script สำหรับโซลูชันนี้ ให้คลิกดูซอร์สโค้ด
ด้านล่าง
ดูซอร์สโค้ด
ก่อนอื่น สคริปต์จะเรียกสเปรดชีตครั้งเดียวเพื่อดึงข้อมูลทั้งหมด
คุณเลือกที่จะอ่านชีตทีละแถวได้ แต่การดำเนินการ JavaScript จะเร็วกว่าการพูดคุยกับบริการอื่นๆ เช่น สเปรดชีต มาก ยิ่งคุณทำการเรียกใช้น้อยลง การดำเนินการก็จะยิ่งเร็วขึ้น ซึ่งมีความสำคัญเนื่องจากสคริปต์แต่ละรายการ
จะรันได้นานสูงสุด 6 นาที
ตัวแปร data
คืออาร์เรย์ 2 มิติของ JavaScript ที่มีค่าทั้งหมดในชีต
newData
คืออาร์เรย์ว่างที่สคริปต์
จะวางแถวที่ไม่ซ้ำกันทั้งหมด
for
ลูปแรกdata
จะวนซ้ำแต่ละแถวในdata
อาร์เรย์ 2 มิติ
สําหรับแต่ละแถว ลูปที่ 2 จะทดสอบว่ามีแถวอื่นที่มีข้อมูลที่ตรงกันอยู่ในอาร์เรย์ newData
หรือไม่ หากไม่ใช่รายการที่ซ้ำกัน ระบบจะส่งแถวไปยังอาร์เรย์ newData
สุดท้าย สคริปต์จะลบเนื้อหาที่มีอยู่ในชีตและแทรกเนื้อหาของอาร์เรย์ newData
การปรับเปลี่ยน
คุณแก้ไขคลังได้มากเท่าที่ต้องการเพื่อให้ตรงกับความต้องการ ด้านล่างนี้คือ
การแก้ไขที่ไม่บังคับ
นำแถวที่มีข้อมูลตรงกันในบางคอลัมน์ออก
แทนที่จะนำแถวที่ตรงกันทั้งหมดออก คุณอาจต้องการนำแถวที่มี
ข้อมูลที่ตรงกันในคอลัมน์เพียง 1 หรือ 2 คอลัมน์ออก โดยคุณสามารถเปลี่ยน
คำสั่งแบบมีเงื่อนไขได้
ในโค้ดตัวอย่าง ให้อัปเดตบรรทัดต่อไปนี้
if(row.join() == newData[j].join()){
duplicate = true;
}
แทนที่บรรทัดด้วยโค้ดต่อไปนี้
if(row[0] == newData[j][0] && row[1] == newData[j][1]){
duplicate = true;
}
คำสั่งแบบมีเงื่อนไขข้างต้นจะค้นหารายการที่ซ้ำกันทุกครั้งที่ 2 แถวมีข้อมูลเดียวกันในคอลัมน์แรกและคอลัมน์ที่ 2 ของชีต
ผู้ร่วมให้ข้อมูล
ตัวอย่างนี้สร้างโดย Romain Vialard ซึ่งเป็น Google Developer Expert ติดตาม
Romain บน Twitter ได้ที่ @romain_vialard
ตัวอย่างนี้ได้รับการดูแลโดย Google โดยได้รับความช่วยเหลือจาก Google Developer Expert
ขั้นตอนถัดไป
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-31 UTC
[null,null,["อัปเดตล่าสุด 2025-08-31 UTC"],[[["\u003cp\u003eThis guide provides step-by-step instructions to create an Apps Script library that removes duplicate rows from Google Sheets data.\u003c/p\u003e\n"],["\u003cp\u003eThe library uses a JavaScript function to identify and remove duplicate rows by comparing all column values within each row.\u003c/p\u003e\n"],["\u003cp\u003eUsers need a Google Account and a web browser to implement this solution, which involves setting up, deploying, and running the script within a spreadsheet.\u003c/p\u003e\n"],["\u003cp\u003eThe provided code can be modified to remove rows based on matching data in specific columns, enhancing the library's functionality.\u003c/p\u003e\n"]]],[],null,["# Library quickstart\n\nBuild an [Apps Script library](/apps-script/guides/libraries) that you can use to remove duplicate rows in spreadsheet data.\n\nObjectives\n----------\n\n- Set up the script.\n- Run the script.\n\nPrerequisites\n-------------\n\nTo use this sample, you need the following prerequisites:\n\n- A Google Account (Google Workspace accounts might require administrator approval).\n- A web browser with access to the internet.\n\nSet up the script\n-----------------\n\nTo build the library, take the following steps:\n\n1. Sign in to your Google Account.\n2. To open the script editor, go to [script.google.com](https://script.google.com/home).\n3. At the top left, click **New project**.\n4. Delete any code in the script editor and paste in the code below.\n\n sheets/removingDuplicates/removingDuplicates.gs \n [View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/sheets/removingDuplicates/removingDuplicates.gs) \n\n ```javascript\n /**\n * Removes duplicate rows from the current sheet.\n */\n function removeDuplicates() {\n const sheet = SpreadsheetApp.getActiveSheet();\n const data = sheet.getDataRange().getValues();\n const uniqueData = {};\n for (let row of data) {\n const key = row.join();\n uniqueData[key] = uniqueData[key] || row;\n }\n sheet.clearContents();\n const newData = Object.values(uniqueData);\n sheet.getRange(1, 1, newData.length, newData[0].length).setValues(newData);\n }\n ```\n5. Click Save .\n\n6. At the top left, click **Untitled project**.\n\n7. Name your script **Remove duplicate rows** and click **Rename**.\n\n8. Click **Deploy** \\\u003e **New deployment**.\n\n9. Next to **Select type** click Enable deployment types\n\n \\\u003e **Library**.\n\n10. Enter a description of the library, such as **Remove duplicate rows**. Anyone\n with access to the library can view this description.\n\n11. Click **Deploy**.\n\n12. At the left, click **Project settings** .\n\n13. Under **IDs**, copy the script ID for use in a later step.\n\nRun the script\n--------------\n\nTo use a library, you must have at least view permissions for its\nApps Script project. Since you created the library, you have the\nrequired permissions\nto use it. If you want to let others use the library, give them view permission\nfor the Apps Script project.\n\nTo use the library, take the following steps:\n\n1. Open a Google Sheets spreadsheet that has data with duplicate rows. To use a sample spreadsheet, [make a copy of the **Sample duplicate rows** spreadsheet](https://docs.google.com/spreadsheets/d/1_Tcb0kokQIYCEz_nWnxUHZp8nwTysjjxucMmVZ0DeSg/copy?usp=sharing).\n2. Click **Extensions** \\\u003e **Apps Script**.\n3. Next to **Libraries** , click Add a library add.\n4. In the **Script ID** section, paste the script ID from the library Apps Script project you copied in the previous section.\n5. Click **Look up**.\n6. In the **Version** section, select **1**.\n7. Click **Add**.\n8. Delete any code in the script editor and paste in the code below.\n\n function runLibrary() {\n Removeduplicaterows.removeDuplicates();\n }\n\n9. In the function dropdown, select **runLibrary**.\n\n10. Click **Run**.\n\n11. Return to the spreadsheet to view the updated data without duplicate rows.\n\nReview the code\n---------------\n\nTo review the Apps Script code for this solution, click **View source code**\nbelow: \n\n#### View the source code\n\n\nFirst, the script makes a single call to the spreadsheet to retrieve all the\ndata. You can choose to read the sheet row by row, but JavaScript operations are\nconsiderably faster than talking to other services like Spreadsheet. The fewer\ncalls you make, the faster it goes. This is important because each script\nexecution has a maximum run time of 6 minutes. \nsheets/removingDuplicates/removingDuplicates.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/sheets/removingDuplicates/removingDuplicates.gs) \n\n```javascript\nconst sheet = SpreadsheetApp.getActiveSheet();\nconst data = sheet.getDataRange().getValues();\n```\n\n\nThe variable `data` is a JavaScript 2-dimensional array that contains\nall the values in the sheet. `newData` is an empty array where the\nscript puts all the non-duplicate rows. \nsheets/removingDuplicates/removingDuplicates.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/sheets/removingDuplicates/removingDuplicates.gs) \n\n```javascript\nconst newData = Object.values(uniqueData);\n```\n\n\nThe first `for` loop iterates over each row in the `data`\n2-dimensional array. For each row, the second loop tests if another row with\nmatching data already exists in the `newData` array. If it's not a\nduplicate, the row is pushed into the `newData` array. \nsheets/removingDuplicates/removingDuplicates.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/sheets/removingDuplicates/removingDuplicates.gs) \n\n```javascript\nuniqueData[key] = uniqueData[key] || row;\n```\n\n\nFinally, the script deletes the existing content of the sheet and inserts\nthe content of the `newData` array. \nsheets/removingDuplicates/removingDuplicates.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/sheets/removingDuplicates/removingDuplicates.gs) \n\n```javascript\nsheet.clearContents();\nconst newData = Object.values(uniqueData);\nsheet.getRange(1, 1, newData.length, newData[0].length).setValues(newData);\n```\n\nModifications\n-------------\n\nYou can edit the library as much as you'd like to fit your needs. Below is an\noptional modification. \n\n#### Remove rows with matching data in some columns\n\n\nInstead of removing rows that match entirely, you might want to remove rows with\nmatching data in just one or two of the columns. To do that, you can change the\nconditional statement.\n\n\nIn the sample code, update the following line: \n\n```transact-sql\n if(row.join() == newData[j].join()){\n duplicate = true;\n }\n```\n\n\nReplace the line with the following code: \n\n```transact-sql\n if(row[0] == newData[j][0] && row[1] == newData[j][1]){\n duplicate = true;\n }\n```\n\n\nThe above conditional statement finds duplicates each time two rows have the\nsame data in the first and second columns of the sheet.\n\nContributors\n------------\n\nThis sample was created by Romain Vialard, a Google Developer Expert. Follow\nRomain on Twitter [@romain_vialard](https://twitter.com/romain_vialard).\n\nThis sample is maintained by Google with the help of Google Developer Experts.\n\nNext steps\n----------\n\n- [Libraries](/apps-script/guides/libraries)\n- [Create and manage deployments](/apps-script/concepts/deployments)"]]