Created
December 10, 2018 08:05
-
-
Save kreja/95ac89f2757aa1efb42e4c548b7a0dd3 to your computer and use it in GitHub Desktop.
Gets and sets values and formulas for a range. - 与Script Lab共享
This file contains 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: Values and formulas | |
description: Gets and sets values and formulas for a range. | |
author: kreja | |
host: EXCEL | |
api_set: {} | |
script: | |
content: | | |
$("#setup").click(() => tryCatch(setup)); | |
async function setup() { | |
await Excel.run(async (context) => { | |
const sheet = await OfficeHelpers.ExcelUtilities | |
.forceCreateSheet(context.workbook, "Sample"); | |
const data = [ | |
["Product", "Qty", "Unit Price"], | |
["Almonds", 2, 7.50], | |
["Coffee", 1, 34.50], | |
["Chocolate", 5, 9.56] | |
]; | |
const range = sheet.getRange("B2:D5"); | |
range.values = data; | |
range.format.autofitColumns(); | |
const header = range.getRow(0); | |
header.format.fill.color = "#4472C4"; | |
header.format.font.color = "white"; | |
sheet.activate(); | |
sheet.protection.protect(); | |
await context.sync(); | |
}); | |
} | |
/** Default helper for invoking an action and handling errors. */ | |
async function tryCatch(callback) { | |
try { | |
await callback(); | |
} | |
catch (error) { | |
OfficeHelpers.UI.notify(error); | |
OfficeHelpers.Utilities.log(error); | |
} | |
} | |
language: typescript | |
template: | |
content: |+ | |
<section class="ms-font-m"> | |
<p>This sample shows how to set and get values and formulas for a range.</p> | |
</section> | |
<section class="setup ms-font-m"> | |
<h3>Set up</h3> | |
<button id="setup" class="ms-Button"> | |
<span class="ms-Button-label">Add sample data</span> | |
</button> | |
</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 | |
office-ui-fabric-js@1.4.0/dist/css/fabric.min.css | |
office-ui-fabric-js@1.4.0/dist/css/fabric.components.min.css | |
core-js@2.4.1/client/core.min.js | |
@types/core-js | |
@microsoft/office-js-helpers@0.7.4/dist/office.helpers.min.js | |
@microsoft/office-js-helpers@0.7.4/dist/office.helpers.d.ts | |
jquery@3.1.1 | |
@types/jquery@3.3.1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment