Skip to content

Instantly share code, notes, and snippets.

@lumine2008
Last active June 4, 2019 06:33
Show Gist options
  • Select an option

  • Save lumine2008/323f4b8f45c07e1704fe4b5eb0640307 to your computer and use it in GitHub Desktop.

Select an option

Save lumine2008/323f4b8f45c07e1704fe4b5eb0640307 to your computer and use it in GitHub Desktop.
Copies data and formatting from one range to another.
name: Copy and paste ranges
description: Copies data and formatting from one range to another.
host: EXCEL
api_set: {}
script:
content: |
$("#setup").click(() => tryCatch(setup));
$("#copyPasteDel").click(() => tryCatch(copyPasteDel));
async function copyPasteDel() {
await Excel.run(async (context) => {
const sheet = context.workbook.worksheets.getItem("Sample");
sheet.getRange("F1").values = [["Copied Range"]];
sheet.getRange("F1").format.autofitColumns();
// copy a range starting at a single cell destination
sheet.getRange("G1").copyFrom("A1:E1");
sheet.getRange("A1:E1").clear();
await context.sync();
});
}
async function setup() {
await Excel.run(async (context) => {
await OfficeHelpers.ExcelUtilities.forceCreateSheet(context.workbook, "Sample");
const sheet = context.workbook.worksheets.getItem("Sample");
sheet.getRange("A1:D1").values = [["3", "5", "7", ""]];
sheet.getRange("A1:D1").format.font.italic = true;
sheet.getRange("A1:D1").format.font.color = "DarkMagenta";
sheet.getRange("E1").formulas = [["=SUM(A1:D1)"]];
sheet.getRange("E1").format.font.bold = true;
sheet.getRange("E1").format.fill.color = "LightGreen";
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 copy data and formatting from one range to another.</p>\n</section>\n<section class=\"setup ms-font-m\">\n\t<h3>Setup</h3>\n\t<button id=\"setup\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Create sample data</span>\n </button>\n</section>\n<section class=\"setup ms-font-m\">\n\t<h3>Try it out</h3>\n\n\t<button id=\"copyPasteDel\" class=\"ms-Button\">\n\t\t <span class=\"ms-Button-label\">Copy Paste and Clear</span>\n\t\t</button>\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
@microsoft/[email protected]/dist/office.helpers.min.js
@microsoft/[email protected]/dist/office.helpers.d.ts
[email protected]
@types/[email protected]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment