I have created excel add-in using office.js. In my add-in, I need to open an existing workbook in my current workbook. I looked into Office.js api’s docs and found that I can achieve my requirement using “addFromBase64” function. They also noted that this function currently only for Public preview and we have to use other cdn for the same. I have written my code considering this point, but when running the code, existing worksheet not being added in my current workbook (nothing is happening) and I am not getting any error.
I am using this add-in on my Excel 2019 (64 bit) for Windows.
This is my code that I have written. Please let me know I am doing anything wrong and please guide me to resolved the same.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
<title>Excel Add-In with Commands Sample</title>
<script src="Scripts/jquery-1.9.1.js" type="text/javascript"></script>
<script src="Scripts/FabricUI/MessageBanner.js" type="text/javascript"></script>
<!--<script src="https://appsforoffice.microsoft.com/lib/1/hosted/office.js" type="text/javascript"></script>-->
<!--To use addFromBase64 function for opening existing workbook in current instance-->
<script src="https://appsforoffice.microsoft.com/lib/beta/hosted/office.js" type="text/javascript"></script>
<!-- To enable offline debugging using a local reference to Office.js, use: -->
<!-- <script src="Scripts/Office/MicrosoftAjax.js" type="text/javascript"></script> -->
<!-- <script src="Scripts/Office/1/office.js" type="text/javascript"></script> -->
<link href="Home.css" rel="stylesheet" type="text/css" />
<script src="Home.js" type="text/javascript"></script>
<!-- For the Office UI Fabric, go to https://aka.ms/office-ui-fabric to learn more. -->
<link rel="stylesheet" href="https://appsforoffice.microsoft.com/fabric/2.1.0/fabric.min.css">
<link rel="stylesheet" href="https://appsforoffice.microsoft.com/fabric/2.1.0/fabric.components.min.css">
<!-- To enable the offline use of Office UI Fabric, use: -->
<!-- link rel="stylesheet" href="Content/fabric.min.css" -->
<!-- link rel="stylesheet" href="Content/fabric.components.min.css" -->
<script>
function insertWorkbook() {
try {
var myFile = document.getElementById("file");
var reader = new FileReader();
reader.onload = (function (theFile) {
return function (e) {
Excel.run(function (context) {
var startIndex = e.target.result.indexOf("base64,");
var mybase64 = e.target.result.substr(startIndex + 7, e.target.result.length);
var sheets = context.workbook.worksheets;
sheets.addFromBase64(
mybase64,
null, // get all the worksheets
Excel.WorksheetPositionType.after, // insert them after the worksheet specified by the next parameter
sheets.getActiveWorksheet()// insert them after the active worksheet
);
return context.sync();
});
};
})(myFile.files[0]);
reader.readAsDataURL(myFile.files[0]);
}
catch (err) {
var e = err;
}
// app.showNotification(document.getElementById(" bro").file);
}
</script>
</head>
<body>
<div id="content-main">
Select existing workbook
</div>
<div>
<input type="file" id="file" onchange="insertWorkbook()" />
</div>
</body>
</html>