Skip to content

Commit d89d528

Browse files
authored
[Excel] Update insert worksheets sample to throw errors correctly (OfficeDev#550)
* Update insert worksheets sample to include try-catch * Change global variable name per feedback * Update excel.xlsx metadata with adjusted insert worksheets methods * Run yarn start
1 parent 9bb71c9 commit d89d528

File tree

3 files changed

+59
-44
lines changed

3 files changed

+59
-44
lines changed

samples/excel/85-preview-apis/workbook-insert-external-worksheets.yaml

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,44 @@ api_set:
77
ExcelAPI: '1.13'
88
script:
99
content: |
10-
$("#file").change(() => tryCatch(insertSheets));
10+
$("#file").change(getBase64);
11+
$("#insert-sheets").click(() => tryCatch(insertSheets));
1112
12-
async function insertSheets() {
13+
let externalWorkbook;
14+
15+
async function getBase64() {
16+
// Retrieve the file and set up an HTML FileReader element.
1317
const myFile = <HTMLInputElement>document.getElementById("file");
1418
const reader = new FileReader();
15-
19+
1620
reader.onload = (event) => {
17-
Excel.run((context) => {
18-
// Remove the metadata before the base64-encoded string.
19-
const startIndex = reader.result.toString().indexOf("base64,");
20-
const workbookContents = reader.result.toString().substr(startIndex + 7);
21-
22-
// Retrieve the workbook.
23-
const workbook = context.workbook;
24-
25-
// Set up the insert options.
26-
var options = {
27-
sheetNamesToInsert: [], // Insert all the worksheets from the source workbook.
28-
positionType: Excel.WorksheetPositionType.after, // Insert after the `relativeTo` sheet.
29-
relativeTo: "Sheet1" }; // The sheet relative to which the other worksheets will be inserted. Used with `positionType`.
30-
31-
// Insert the workbook.
32-
workbook.insertWorksheetsFromBase64(workbookContents, options);
33-
return context.sync();
34-
});
21+
// Remove the metadata before the base64-encoded string.
22+
const startIndex = reader.result.toString().indexOf("base64,");
23+
externalWorkbook = reader.result.toString().substr(startIndex + 7);
3524
};
36-
25+
3726
// Read the file as a data URL so that we can parse the base64-encoded string.
3827
reader.readAsDataURL(myFile.files[0]);
3928
}
4029
30+
async function insertSheets() {
31+
await Excel.run(async (context) => {
32+
// Retrieve the source workbook.
33+
const workbook = context.workbook;
34+
35+
// Set up the insert options.
36+
var options = {
37+
sheetNamesToInsert: [], // Insert all the worksheets from the source workbook.
38+
positionType: Excel.WorksheetPositionType.after, // Insert after the `relativeTo` sheet.
39+
relativeTo: "Sheet1" // The sheet relative to which the other worksheets will be inserted. Used with `positionType`.
40+
};
41+
42+
// Insert the new worksheets.
43+
workbook.insertWorksheetsFromBase64(externalWorkbook, options);
44+
await context.sync();
45+
});
46+
}
47+
4148
/** Default helper for invoking an action and handling errors. */
4249
async function tryCatch(callback) {
4350
try {
@@ -60,6 +67,11 @@ template:
6067
<form>
6168
<input type="file" id="file" />
6269
</form>
70+
<br>
71+
<p>Insert the worksheets from the selected workbook.</p>
72+
<button id="insert-sheets" class="ms-Button">
73+
<span class="ms-Button-label">Insert sheets</span>
74+
</button>
6375
</section>
6476
language: html
6577
style:
@@ -78,12 +90,9 @@ style:
7890
libraries: |
7991
https://appsforoffice.microsoft.com/lib/beta/hosted/office.js
8092
@types/office-js-preview
81-
8293
office-ui-fabric-js@1.4.0/dist/css/fabric.min.css
8394
office-ui-fabric-js@1.4.0/dist/css/fabric.components.min.css
84-
8595
core-js@2.4.1/client/core.min.js
8696
@types/core-js
87-
8897
jquery@3.1.1
89-
@types/jquery@3.3.1
98+
@types/jquery@3.3.1
-4 Bytes
Binary file not shown.

snippet-extractor-output/snippets.yaml

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4632,37 +4632,43 @@
46324632
// Link to full sample:
46334633
https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/85-preview-apis/workbook-insert-external-worksheets.yaml
46344634
4635+
// Retrieve the file and set up an HTML FileReader element.
4636+
46354637
const myFile = <HTMLInputElement>document.getElementById("file");
46364638
46374639
const reader = new FileReader();
46384640
46394641
46404642
reader.onload = (event) => {
4641-
Excel.run((context) => {
4642-
// Remove the metadata before the base64-encoded string.
4643-
const startIndex = reader.result.toString().indexOf("base64,");
4644-
const workbookContents = reader.result.toString().substr(startIndex + 7);
4645-
4646-
// Retrieve the workbook.
4647-
const workbook = context.workbook;
4648-
4649-
// Set up the insert options.
4650-
var options = {
4651-
sheetNamesToInsert: [], // Insert all the worksheets from the source workbook.
4652-
positionType: Excel.WorksheetPositionType.after, // Insert after the `relativeTo` sheet.
4653-
relativeTo: "Sheet1" }; // The sheet relative to which the other worksheets will be inserted. Used with `positionType`.
4654-
4655-
// Insert the workbook.
4656-
workbook.insertWorksheetsFromBase64(workbookContents, options);
4657-
return context.sync();
4658-
});
4643+
// Remove the metadata before the base64-encoded string.
4644+
const startIndex = reader.result.toString().indexOf("base64,");
4645+
externalWorkbook = reader.result.toString().substr(startIndex + 7);
46594646
};
46604647
46614648
46624649
// Read the file as a data URL so that we can parse the base64-encoded
46634650
string.
46644651
46654652
reader.readAsDataURL(myFile.files[0]);
4653+
- >-
4654+
// Link to full sample:
4655+
https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/85-preview-apis/workbook-insert-external-worksheets.yaml
4656+
4657+
await Excel.run(async (context) => {
4658+
// Retrieve the source workbook.
4659+
const workbook = context.workbook;
4660+
4661+
// Set up the insert options.
4662+
var options = {
4663+
sheetNamesToInsert: [], // Insert all the worksheets from the source workbook.
4664+
positionType: Excel.WorksheetPositionType.after, // Insert after the `relativeTo` sheet.
4665+
relativeTo: "Sheet1" // The sheet relative to which the other worksheets will be inserted. Used with `positionType`.
4666+
};
4667+
4668+
// Insert the new worksheets.
4669+
workbook.insertWorksheetsFromBase64(externalWorkbook, options);
4670+
await context.sync();
4671+
});
46664672
'Excel.Workbook#pivotTables:member':
46674673
- >-
46684674
// Link to full sample:

0 commit comments

Comments
 (0)