Created
December 21, 2020 16:24
-
-
Save jag-cloudextend/0bdf729dd97267c2cd4a098ee27aa640 to your computer and use it in GitHub Desktop.
Shows how to work with dates by using the Moment JavaScript library with the Moment-MSDate plug-in.
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: Working with dates | |
description: >- | |
Shows how to work with dates by using the Moment JavaScript library with the | |
Moment-MSDate plug-in. | |
host: EXCEL | |
api_set: {} | |
script: | |
content: | | |
declare var moment: any; | |
$("#setup").click(() => tryCatch(setup)); | |
$("#get-date-value").click(() => tryCatch(getDateValue)); | |
async function getDateValue() { | |
await Excel.run(async (context) => { | |
const sheet = context.workbook.worksheets.getItem("Sample"); | |
const dateRange = sheet.getRange("A2"); | |
dateRange.load("values"); | |
await context.sync(); | |
const nowMS = dateRange.values[0][0]; | |
console.log(`get (MSDate): ${nowMS}`); | |
const nowMoment = moment.fromOADate(nowMS); | |
console.log(`get (moment): ${JSON.stringify(nowMoment)}`); | |
const now = nowMoment.unix(); | |
console.log(`get (timestamp): ${now}`); | |
}); | |
} | |
async function setup() { | |
await Excel.run(async (context) => { | |
context.workbook.worksheets.getItemOrNullObject("Sample").delete(); | |
const sheet = context.workbook.worksheets.add("Sample"); | |
const table = sheet.tables.add("A1:A6", true); | |
table.getHeaderRowRange().values = [["Dates"]]; | |
const dateRange2 = sheet.getRange("A2:A6"); | |
dateRange2.numberFormat = [["dd/mm/yyyy;@"]]; | |
context.workbook.application.calculate(Excel.CalculationType.recalculate); | |
await context.sync(); | |
table.rows.add(0, [["14/02/2020"], ["15/02/2020"], ["16/02/2020"], ["17/02/2020"], ["18/02/2020"]]); | |
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"> | |
<p>Demo of issue with column formating with dates</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> | |
<section class="samples ms-font-m"> | |
<h3>Try it out</h3> | |
<button id="get-date-value" class="ms-Button"> | |
<span class="ms-Button-label">Get date value</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 | |
jquery@3.1.1 | |
@types/jquery@3.3.1 | |
moment@2.18.1 | |
moment-msdate@0.2.2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment