Created
April 25, 2021 15:56
-
-
Save globalq/9fe30ccc6c273bc8fabd9a795e289543 to your computer and use it in GitHub Desktop.
Removes duplicate entries from a range.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Remove duplicates | |
description: Removes duplicate entries from a range. | |
host: EXCEL | |
api_set: {} | |
script: | |
content: | | |
$("#setup").click(() => tryCatch(setup)); | |
$("#delete-name").click(() => tryCatch(deleteName)); | |
$("#delete-distributor").click(() => tryCatch(deleteDistributor)); | |
async function deleteName() { | |
await Excel.run(async (context) => { | |
const sheet = context.workbook.worksheets.getItem("Sample"); | |
const range = sheet.getRange("B2:D11"); | |
const deleteResult = range.removeDuplicates([0], true); | |
deleteResult.load(); | |
await context.sync(); | |
console.log(deleteResult.removed + " entries with duplicate names removed."); | |
console.log(deleteResult.uniqueRemaining + " entries with unique names remain in the range."); | |
}); | |
} | |
async function deleteDistributor() { | |
await Excel.run(async (context) => { | |
const sheet = context.workbook.worksheets.getItem("Sample"); | |
const range = sheet.getRange("B2:D11"); | |
const deleteResult = range.removeDuplicates([1], true); | |
deleteResult.load(); | |
await context.sync(); | |
console.log(deleteResult.removed + " entries with duplicate distributors removed."); | |
console.log(deleteResult.uniqueRemaining + " entries with unique distributors remain in the range."); | |
}); | |
} | |
async function setup() { | |
await Excel.run(async (context) => { | |
context.workbook.worksheets.getItemOrNullObject("Sample").delete(); | |
const sheet = context.workbook.worksheets.add("Sample"); | |
const data = [ | |
["Product Name", "Distributor", "Order Amount"], | |
["Onions", "Contoso Produce", 3], | |
["Potatoes", "Contoso Produce", 9], | |
["Red Wine", "Coho Vineyard", 7], | |
["Onions", "Best For You Organics Company", 8], | |
["Arugula", "Best For You Organics Company", 7], | |
["Potatoes", "Contoso Produce", 12], | |
["Red Wine", "Coho Vineyard", 3], | |
["Onions", "Contoso Produce", 9], | |
["Arugula", "Best For You Organics Company", 4] | |
]; | |
const range = sheet.getRange("B2:D11"); | |
range.values = data; | |
range.format.autofitColumns(); | |
const header = range.getRow(0); | |
header.format.fill.color = "#4472C4"; | |
header.format.font.color = "white"; | |
sheet.activate(); | |
await context.sync(); | |
}); | |
} | |
/** Default helper for invoking an action and handling errors. */ | |
async function tryCatch(callback) { | |
try { | |
await callback(); | |
} catch (error) { | |
// Note: In a production add-in, you'd want to notify the user through your add-in's UI. | |
console.error(error); | |
} | |
} | |
language: typescript | |
template: | |
content: "<section class=\"ms-font-m\">\n\t<p>This sample shows how to remove rows with duplicate column values from a range.</p>\n</section>\n\n<section class=\"setup ms-font-m\">\n\t<h3>Set up</h3>\n\t<button id=\"setup\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Add sample data</span>\n </button>\n</section>\n\n<section class=\"samples ms-font-m\">\n\t<h3>Try it out</h3>\n\t<button id=\"delete-name\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Delete product name duplicates</span>\n </button>\n\t<p>\n\t\t<button id=\"delete-distributor\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Delete distributor duplicates</span>\n </button>\n\t\t<p>\n\t\t\t<p class=\"ms-font-m\">Note that blank cells are considered in the remove duplicate checks. Since the\n\t\t\t\tduplicates are removed from the entire original range, the numbers logged to the console may be higher\n\t\t\t\tthan expected.</p>\n</section>" | |
language: html | |
style: | |
content: | | |
section.samples { | |
margin-top: 20px; | |
} | |
section.samples .ms-Button, section.setup .ms-Button { | |
display: block; | |
margin-bottom: 5px; | |
margin-left: 20px; | |
min-width: 80px; | |
} | |
language: css | |
libraries: | | |
https://appsforoffice.microsoft.com/lib/1/hosted/office.js | |
@types/office-js | |
[email protected]/dist/css/fabric.min.css | |
[email protected]/dist/css/fabric.components.min.css | |
[email protected]/client/core.min.js | |
@types/core-js | |
[email protected] | |
@types/[email protected] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment