라이브러리 빠른 시작
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
스프레드시트 데이터에서 중복 행을 삭제하는 데 사용할 수 있는 Apps Script 라이브러리를 빌드합니다.
목표
- 스크립트를 설정합니다.
- 스크립트를 실행합니다.
기본 요건
이 샘플을 사용하려면 다음 기본 요건이 필요합니다.
- Google 계정 (Google Workspace 계정의 경우 관리자 승인이 필요할 수 있음)
- 인터넷에 액세스할 수 있는 웹브라우저
스크립트 설정
라이브러리를 빌드하려면 다음 단계를 따르세요.
- Google 계정에 로그인합니다.
- 스크립트 편집기를 열려면 script.google.com으로 이동합니다.
- 왼쪽 상단에서 새 프로젝트를 클릭합니다.
스크립트 편집기에서 코드를 삭제하고 아래 코드를 붙여넣습니다.
저장
를 클릭합니다.
왼쪽 상단에서 제목 없는 프로젝트를 클릭합니다.
스크립트 이름을 Remove duplicate rows로 지정하고 Rename을 클릭합니다.
배포 > 새 배포를 클릭합니다.
유형 선택 옆에 있는 배포 유형 사용 설정
> 라이브러리를 클릭합니다.
라이브러리에 대한 설명(예: 중복 행 삭제)을 입력합니다. 라이브러리에 액세스할 수 있는 사용자는 누구나 이 설명을 볼 수 있습니다.
배포를 클릭합니다.
왼쪽에서 프로젝트 설정
을 클릭합니다.
ID에서 나중에 사용할 스크립트 ID를 복사합니다.
스크립트 실행
라이브러리를 사용하려면 해당 라이브러리의 Apps Script 프로젝트에 대한 보기 권한이 있어야 합니다. 라이브러리를 만든 사용자에게는 라이브러리를 사용할 수 있는 필수 권한이 있습니다. 다른 사용자가 라이브러리를 사용하도록 하려면 Apps Script 프로젝트에 대한 보기 권한을 부여하세요.
라이브러리를 사용하려면 다음 단계를 따르세요.
- 중복 행이 있는 데이터가 포함된 Google Sheets 스프레드시트를 엽니다. 샘플 스프레드시트를 사용하려면 샘플 중복 행 스프레드시트의 사본을 만드세요.
- 확장 프로그램 > Apps Script를 클릭합니다.
- 라이브러리 옆에 있는 라이브러리 추가 add를 클릭합니다.
- 스크립트 ID 섹션에 이전 섹션에서 복사한 라이브러리 Apps Script 프로젝트의 스크립트 ID를 붙여넣습니다.
- 조회를 클릭합니다.
- 버전 섹션에서 1을 선택합니다.
- 추가를 클릭합니다.
스크립트 편집기에서 코드를 삭제하고 아래 코드를 붙여넣습니다.
function runLibrary() {
Removeduplicaterows.removeDuplicates();
}
함수 드롭다운에서 runLibrary를 선택합니다.
실행을 클릭합니다.
스프레드시트로 돌아가 중복 행이 없는 업데이트된 데이터를 확인합니다.
코드 검토
이 솔루션의 Apps Script 코드를 검토하려면 아래의 소스 코드 보기를 클릭하세요.
소스 코드 보기
먼저 스크립트는 스프레드시트에 단일 호출을 실행하여 모든 데이터를 가져옵니다. 시트를 행별로 읽을 수도 있지만 JavaScript 작업은 스프레드시트와 같은 다른 서비스와 통신하는 것보다 훨씬 빠릅니다. 호출 수가 적을수록 속도가 빨라집니다. 각 스크립트 실행의 최대 실행 시간은 6분이므로 이 점이 중요합니다.
data
변수는 시트의 모든 값이 포함된 JavaScript 2차원 배열입니다. newData
는 스크립트가 중복되지 않은 모든 행을 배치하는 빈 배열입니다.
첫 번째 for
루프는 data
2차원 배열의 각 행을 반복합니다. 각 행에 대해 두 번째 루프는 일치하는 데이터가 있는 다른 행이 newData
배열에 이미 있는지 테스트합니다. 중복이 아니면 행이 newData
배열로 푸시됩니다.
마지막으로 스크립트는 시트의 기존 콘텐츠를 삭제하고 newData
배열의 콘텐츠를 삽입합니다.
수정사항
필요에 따라 라이브러리를 원하는 만큼 수정할 수 있습니다. 아래는 선택적 수정사항입니다.
일부 열에서 데이터가 일치하는 행 삭제
완전히 일치하는 행을 삭제하는 대신 하나 또는 두 개의 열에서 일치하는 데이터가 있는 행을 삭제할 수 있습니다. 이렇게 하려면 조건문을 변경하면 됩니다.
샘플 코드에서 다음 줄을 업데이트합니다.
if(row.join() == newData[j].join()){
duplicate = true;
}
이 줄을 다음 코드로 바꿉니다.
if(row[0] == newData[j][0] && row[1] == newData[j][1]){
duplicate = true;
}
위의 조건문은 두 행의 시트 첫 번째 열과 두 번째 열에 동일한 데이터가 있을 때마다 중복을 찾습니다.
참여자
이 샘플은 Google Developer Expert인 Romain Vialard가 만들었습니다. Twitter에서 Romain을 팔로우하세요(@romain_vialard).
이 샘플은 Google Developer Expert의 도움을 받아 Google에서 유지관리합니다.
다음 단계
달리 명시되지 않는 한 이 페이지의 콘텐츠에는 Creative Commons Attribution 4.0 라이선스에 따라 라이선스가 부여되며, 코드 샘플에는 Apache 2.0 라이선스에 따라 라이선스가 부여됩니다. 자세한 내용은 Google Developers 사이트 정책을 참조하세요. 자바는 Oracle 및/또는 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)"]]