Skip to content

Instantly share code, notes, and snippets.

@crazygao
Created March 28, 2019 11:11
Show Gist options
  • Save crazygao/c09e44b57cb5e4644c3cd76424108b41 to your computer and use it in GitHub Desktop.
Save crazygao/c09e44b57cb5e4644c3cd76424108b41 to your computer and use it in GitHub Desktop.
Create a new snippet from a blank template.
name: Blank snippet (2)
description: Create a new snippet from a blank template.
host: EXCEL
api_set: {}
script:
content: |
$("#run").click(() => tryCatch(run));
var shapes = {
initializeByWorksheet: async function (context, worksheetId) {
var worksheet = context.workbook.worksheets.getItem(worksheetId);
var lshapes = worksheet.shapes;
lshapes.load("items");
await context.sync();
var llength = lshapes.items.length;
if (llength > 0) {
lshapes.items.forEach(function (lshape) { // there should not be too many shapes...
lshape.load("id");
});
await context.sync();
if (shapes.items[worksheetId] == null) {
shapes.items[worksheetId] = {};
}
lshapes.items.forEach(function (lshape) {
shapes.items[worksheetId][lshape.id] = lshape;
});
}
console.log("initialize finish");
return llength;
},
items: {
},
get_shapeid: function (worksheetid, shapeId) {
if (this.items[worksheetid] != null) {
return this.items[worksheetid][shapeId]; // null or shape
} else {
return -1;
}
},
switch_shape: async function (context, worksheetid, shapeId) {
var myshape = this.get_shapeid(worksheetid, shapeId);
// switch between "✓" and empty
if (myshape.textFrame.textRange.text == "✓") {
myshape.textFrame.textRange.text = "";
myshape.onAction = "onCheckBoxClicked"
console.log("true");
} else {
myshape.textFrame.textRange.text = "✓";
console.log("false");
}
await context.sync();
},
register_onCheckBoxClicked: async function (context, worksheetid) {
for (var property in this.items[worksheetid]) {
this.items[worksheetid][property].onAction = "onCheckBoxClicked";
}
await context.sync();
}
};
async function run() {
await Excel.run(async (context) => {
const sheet = context.workbook.worksheets.getActiveWorksheet();
sheet.load("id");
await context.sync();
await shapes.initializeByWorksheet(context, sheet.id);
// for (var property in shapes.items[sheet.id]) {
// shapes.items[sheet.id][property].onActivated.add(async function ChangeShape(args) {
// var myshape = shapes.get_shapeid(args.worksheetId, args.shapeId);
// // switch between "✓" and empty
// if (myshape.textFrame.textRange.text == "✓") {
// myshape.textFrame.textRange.text = "";
// } else {
// myshape.textFrame.textRange.text = "✓";
// }
// });
// await context.sync();
// }
await shapes.register_onCheckBoxClicked(context, sheet.id);
});
}
/** 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="run" class="ms-Button">
<span class="ms-Button-label">Run</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/beta/hosted/office.js
https://appsforoffice.microsoft.com/lib/beta/hosted/office.d.ts
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
name: CheckBox
description: Create a new snippet from a blank template.
host: EXCEL
api_set: {}
script:
content: |
$("#run").click(() => tryCatch(run));
var shapes = {
initializeByWorksheet: async function (context, worksheetId) {
var worksheet = context.workbook.worksheets.getItem(worksheetId);
var lshapes = worksheet.shapes;
lshapes.load("items");
await context.sync();
var llength = lshapes.items.length;
if (llength > 0) {
lshapes.items.forEach(function (lshape) { // there should not be too many shapes...
lshape.load("id");
});
await context.sync();
if (shapes.items[worksheetId] == null) {
shapes.items[worksheetId] = {};
}
lshapes.items.forEach(function (lshape) {
shapes.items[worksheetId][lshape.id] = lshape;
});
}
console.log("initialize finish");
return llength;
},
items: {
},
get_shapeid: function (worksheetid, shapeId) {
if (this.items[worksheetid] != null) {
return this.items[worksheetid][shapeId]; // null or shape
} else {
return -1;
}
},
switch_shape: async function (context, worksheetid, shapeId) {
var myshape = this.get_shapeid(worksheetid, shapeId);
// switch between "✓" and empty
if (myshape.textFrame.textRange.text == "✓") {
myshape.textFrame.textRange.text = "";
myshape.onAction = "onCheckBoxClicked"
console.log("true");
} else {
myshape.textFrame.textRange.text = "✓";
console.log("false");
}
await context.sync();
},
register_onCheckBoxClicked: async function (context, worksheetid) {
for (var property in this.items[worksheetid]) {
this.items[worksheetid][property].onAction = "onCheckBoxClicked";
}
await context.sync();
}
};
async function run() {
await Excel.run(async (context) => {
const sheet = context.workbook.worksheets.getActiveWorksheet();
sheet.load("id");
await context.sync();
await shapes.initializeByWorksheet(context, sheet.id);
// for (var property in shapes.items[sheet.id]) {
// shapes.items[sheet.id][property].onActivated.add(async function ChangeShape(args) {
// var myshape = shapes.get_shapeid(args.worksheetId, args.shapeId);
// // switch between "✓" and empty
// if (myshape.textFrame.textRange.text == "✓") {
// myshape.textFrame.textRange.text = "";
// } else {
// myshape.textFrame.textRange.text = "✓";
// }
// });
// await context.sync();
// }
await shapes.register_onCheckBoxClicked(context, sheet.id);
});
}
/** 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="run" class="ms-Button">
<span class="ms-Button-label">Run</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/beta/hosted/office.js
https://appsforoffice.microsoft.com/lib/beta/hosted/office.d.ts
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