लाइब्रेरी क्विकस्टार्ट
संग्रह की मदद से व्यवस्थित रहें
अपनी प्राथमिकताओं के आधार पर, कॉन्टेंट को सेव करें और कैटगरी में बांटें.
एक Apps Script लाइब्रेरी बनाएं, जिसका इस्तेमाल स्प्रेडशीट के डेटा में मौजूद डुप्लीकेट पंक्तियों को हटाने के लिए किया जा सकता है.
मकसद
- स्क्रिप्ट सेट अप करें.
- स्क्रिप्ट चलाएं.
ज़रूरी शर्तें
इस सैंपल का इस्तेमाल करने के लिए, आपको इन ज़रूरी शर्तों को पूरा करना होगा:
- Google खाता (Google Workspace खातों के लिए, एडमिन की अनुमति ज़रूरी हो सकती है).
- इंटरनेट ऐक्सेस करने की सुविधा वाला वेब ब्राउज़र.
स्क्रिप्ट सेट अप करना
लाइब्रेरी बनाने के लिए, यह तरीका अपनाएं:
- अपने Google खाते में साइन इन करें.
- स्क्रिप्ट एडिटर खोलने के लिए, script.google.com पर जाएं.
- सबसे ऊपर बाईं ओर, नया प्रोजेक्ट पर क्लिक करें.
स्क्रिप्ट एडिटर में मौजूद कोड मिटाएं और नीचे दिया गया कोड चिपकाएं.
सेव करें
पर क्लिक करें.
सबसे ऊपर बाईं ओर, बिना टाइटल वाला प्रोजेक्ट पर क्लिक करें.
अपनी स्क्रिप्ट का नाम डुप्लीकेट लाइनें हटाएं रखें. इसके बाद, नाम बदलें पर क्लिक करें.
डिप्लॉय करें > नया डिप्लॉयमेंट पर क्लिक करें.
टाइप चुनें के बगल में, डिप्लॉयमेंट टाइप चालू करें
> लाइब्रेरी पर क्लिक करें.
लाइब्रेरी का ब्यौरा डालें. जैसे, डुप्लीकेट पंक्तियां हटाएं. जिसके पास लाइब्रेरी का ऐक्सेस है वह इस ब्यौरे को देख सकता है.
डिप्लॉय करें पर क्लिक करें.
बाईं ओर, प्रोजेक्ट सेटिंग
पर क्लिक करें.
आईडी में जाकर, स्क्रिप्ट आईडी कॉपी करें, ताकि बाद में इसका इस्तेमाल किया जा सके.
स्क्रिप्ट चलाना
किसी लाइब्रेरी का इस्तेमाल करने के लिए, आपके पास उसके Apps Script प्रोजेक्ट को देखने की अनुमति होनी चाहिए. लाइब्रेरी बनाने वाले व्यक्ति के पास, उसे इस्तेमाल करने की ज़रूरी अनुमतियां होती हैं. अगर आपको दूसरों को लाइब्रेरी इस्तेमाल करने की अनुमति देनी है, तो उन्हें Apps Script प्रोजेक्ट के लिए, देखने की अनुमति दें.
लाइब्रेरी का इस्तेमाल करने के लिए, यह तरीका अपनाएं:
- Google Sheets की वह स्प्रेडशीट खोलें जिसमें डुप्लीकेट पंक्तियों वाला डेटा मौजूद है. सैंपल स्प्रेडशीट का इस्तेमाल करने के लिए, डुप्लीकेट पंक्तियों का सैंपल स्प्रेडशीट की कॉपी बनाएं.
- एक्सटेंशन > Apps Script पर क्लिक करें.
- लाइब्रेरी के बगल में, लाइब्रेरी जोड़ें
add पर क्लिक करें.
- स्क्रिप्ट आईडी सेक्शन में, लाइब्रेरी के Apps Script प्रोजेक्ट से कॉपी किया गया स्क्रिप्ट आईडी चिपकाएं.
- खोजें पर क्लिक करें.
- वर्शन सेक्शन में, 1 चुनें.
- जोड़ें पर क्लिक करें.
स्क्रिप्ट एडिटर में मौजूद कोड मिटाएं और नीचे दिया गया कोड चिपकाएं.
function runLibrary() {
Removeduplicaterows.removeDuplicates();
}
फ़ंक्शन ड्रॉपडाउन में, runLibrary चुनें.
चलाएं पर क्लिक करें.
डुप्लीकेट पंक्तियों के बिना अपडेट किया गया डेटा देखने के लिए, स्प्रेडशीट पर वापस जाएं.
कोड की समीक्षा करना
इस समाधान के लिए, Apps Script कोड की समीक्षा करने के लिए, यहां दिए गए सोर्स कोड देखें पर क्लिक करें:
सोर्स कोड देखना
सबसे पहले, स्क्रिप्ट स्प्रेडशीट को एक कॉल करती है, ताकि सारा डेटा वापस मिल सके. शीट की हर लाइन को एक-एक करके पढ़ने का विकल्प चुना जा सकता है. हालांकि, JavaScript की कार्रवाइयां, Spreadsheet जैसी अन्य सेवाओं से बातचीत करने की तुलना में काफ़ी तेज़ होती हैं. जितने कम कॉल किए जाएंगे, उतनी ही तेज़ी से यह प्रोसेस पूरी होगी. यह ज़रूरी है, क्योंकि हर स्क्रिप्ट को ज़्यादा से ज़्यादा छह मिनट तक चलाया जा सकता है.
वैरिएबल data
एक JavaScript 2-डाइमेंशनल ऐरे है. इसमें शीट की सभी वैल्यू शामिल होती हैं. newData
एक खाली अरे है. इसमें स्क्रिप्ट, डुप्लीकेट नहीं की गई सभी पंक्तियों को डालती है.
पहला for
लूप, data
दो डाइमेंशन वाले ऐरे की हर लाइन पर काम करता है. हर लाइन के लिए, दूसरा लूप यह जांच करता है कि 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, Google Developer Experts की मदद से मैनेज करता है.
अगले चरण
जब तक कुछ अलग से न बताया जाए, तब तक इस पेज की सामग्री को Creative Commons Attribution 4.0 License के तहत और कोड के नमूनों को Apache 2.0 License के तहत लाइसेंस मिला है. ज़्यादा जानकारी के लिए, Google Developers साइट नीतियां देखें. Oracle और/या इससे जुड़ी हुई कंपनियों का, Java एक रजिस्टर किया हुआ ट्रेडमार्क है.
आखिरी बार 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)"]]