Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create a new snippet from a blank template.
name: Date et Heure
description: Create a new snippet from a blank template.
host: EXCEL
api_set: {}
script:
content: |
$("#date").click(() => tryCatch(dateA1));
$("#heure").click(() => tryCatch(heureB1));
$("#horloge").click(() => tryCatch(horloge));
$("#datemoment").click(() => tryCatch(dateAvecMoment));
$("#heuremoment").click(() => tryCatch(heureAvecMoment));
$("#dans25jours").click(() => tryCatch(dans25jours));
$("#ilya6heures30").click(() => tryCatch(ilya6heures30));
async function dateA1() {
await Excel.run(async (context) => {
const feuille = context.workbook.worksheets.getActiveWorksheet();
let d = new Date();
let date = d.getDate() + "/" + (d.getMonth() + 1) + "/" + d.getFullYear();
feuille.getRange("A1").values = [[date]];
await context.sync();
});
}
async function heureB1() {
await Excel.run(async (context) => {
const feuille = context.workbook.worksheets.getActiveWorksheet();
let d = new Date();
let date = d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
feuille.getRange("B1").values = [[date]];
await context.sync();
});
}
function horloge() {
setInterval(heureB1, 1000);
}
async function dateAvecMoment() {
await Excel.run(async (context) => {
const feuille = context.workbook.worksheets.getActiveWorksheet();
let date = moment().format("L");
feuille.getRange("A2").numberFormat = [["dddd D/MM/YYYY"]];
feuille.getRange("A2").values = [[date]];
});
}
async function heureAvecMoment() {
await Excel.run(async (context) => {
const feuille = context.workbook.worksheets.getActiveWorksheet();
let date = moment().format("LTS");
feuille.getRange("B2").numberFormat = [["hh:mm:ss"]];
feuille.getRange("B2").values = [[date]];
});
}
async function dans25jours() {
await Excel.run(async (context) => {
const feuille = context.workbook.worksheets.getActiveWorksheet();
let date = moment()
.add(25, "days")
.calendar();
feuille.getRange("A3").numberFormat = [["dddd D/MM/YYYY"]];
feuille.getRange("A3").values = [[date]];
await context.sync();
});
}
async function ilya6heures30() {
await Excel.run(async (context) => {
const feuille = context.workbook.worksheets.getActiveWorksheet();
let date = moment()
.subtract(6.5, "hours")
.calendar();
feuille.getRange("B3").numberFormat = [["hh:mm:ss"]];
feuille.getRange("B3").values = [[date]];
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: |-
<button id="date" class="ms-Button">
<span class="ms-Button-label">Afficher la date dans A1</span>
</button>
<button id="heure" class="ms-Button">
<span class="ms-Button-label">Afficher l'heure' dans B1</span>
</button>
<button id="horloge" class="ms-Button">
<span class="ms-Button-label">Lancer l'horloge</span>
</button>
<button id="datemoment" class="ms-Button">
<span class="ms-Button-label">Date avec Moment.js</span>
</button>
<button id="heuremoment" class="ms-Button">
<span class="ms-Button-label">Heure avec Moment.js</span>
</button>
<button id="dans25jours" class="ms-Button">
<span class="ms-Button-label">Dans 25 jours</span>
</button>
<button id="ilya6heures30" class="ms-Button">
<span class="ms-Button-label">Il y a 6 heures 30</span>
</button>
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
https://momentjs.com/downloads/moment-with-locales.min.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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment