ライブラリのクイックスタート
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
スプレッドシートのデータから重複する行を削除するために使用できる Apps Script ライブラリを構築します。
目標
- スクリプトを設定します。
- スクリプトを実行します。
前提条件
このサンプルを使用するには、次の前提条件を満たしている必要があります。
- Google アカウント(Google Workspace アカウントの場合、管理者の承認が必要となる可能性があります)。
- インターネットにアクセスできるウェブブラウザ。
スクリプトを設定する
ライブラリをビルドする手順は次のとおりです。
- Google アカウントにログインします。
- スクリプト エディタを開くには、script.google.com にアクセスします。
- 左上の [新しいプロジェクト] をクリックします。
スクリプト エディタ内のコードを削除して、以下のコードを貼り付けます。
[保存] をクリックします。
左上の [無題のプロジェクト] をクリックします。
スクリプトに「Remove duplicate rows」という名前を付けて、[名前を変更] をクリックします。
[Deploy] > [New deployment] をクリックします。
[種類の選択] の横にある [デプロイタイプを有効にする]
> [ライブラリ] をクリックします。
ライブラリの説明(重複する行を削除するなど)を入力します。ライブラリにアクセスできるユーザーは、この説明を表示できます。
[デプロイ] をクリックします。
左側の [プロジェクト設定]
をクリックします。
[ID] で、後の手順で使用するスクリプト ID をコピーします。
スクリプトを実行する
ライブラリを使用するには、その Apps Script プロジェクトに対する閲覧権限が必要です。ライブラリを作成したユーザーには、そのライブラリを使用するために必要な権限が付与されています。他のユーザーがライブラリを使用できるようにするには、Apps Script プロジェクトの閲覧権限を付与します。
ライブラリを使用する手順は次のとおりです。
- 重複する行を含むデータがある Google スプレッドシートを開きます。サンプル スプレッドシートを使用するには、重複する行のサンプル スプレッドシートのコピーを作成します。
- [拡張機能] > [Apps Script] をクリックします。
- [ライブラリ] の横にある [ライブラリを追加] add をクリックします。
- [スクリプト ID] セクションに、前のセクションでコピーしたライブラリ Apps Script プロジェクトのスクリプト ID を貼り付けます。
- [Look up] をクリックします。
- [バージョン] セクションで [1] を選択します。
- [追加] をクリックします。
スクリプト エディタ内のコードを削除して、以下のコードを貼り付けます。
function runLibrary() {
Removeduplicaterows.removeDuplicates();
}
関数プルダウンで、[runLibrary] を選択します。
[実行] をクリックします。
スプレッドシートに戻り、重複する行のない更新されたデータを表示します。
コードを確認する
このソリューションの Apps Script コードを確認するには、下の [ソースコードを表示] をクリックします。
ソースコードの表示
まず、スクリプトはスプレッドシートに 1 回だけ呼び出しを行い、すべてのデータを取得します。シートを行ごとに読み取ることもできますが、JavaScript オペレーションは、スプレッドシートなどの他のサービスとの通信よりもはるかに高速です。呼び出しの回数が少ないほど、処理が速くなります。これは、各スクリプトの最大実行時間が 6 分であるため重要です。
変数 data
は、シート内のすべての値を含む JavaScript の 2 次元配列です。newData
は、スクリプトが重複しないすべての行を配置する空の配列です。
最初の for
ループは、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;
}
上記の条件ステートメントは、シートの 1 列目と 2 列目に同じデータが 2 行あるたびに重複を見つけます。
寄稿者
このサンプルは、Google デベロッパー エキスパートの Romain Vialard 氏が作成しました。Twitter で @romain_vialard をフォローしてください。
このサンプルは、Google デベロッパー エキスパートの協力を得て Google が管理しています。
次のステップ
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 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)"]]