1

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>
umesh shukla
  • 139
  • 3
  • 13

1 Answers1

1

Yes, addFromBase64 API has a beta preview version for Win32 and Mac. Due to our release criteria, this API is not able to release, because Excel online doesn't support insert sheets from external workbook. Now we are investigating the options to support Excel online in addFromBase64 API.

We would like to confirm with you some questions

  • Do you need to support Excel Online?
  • What kind of content type do you want to support by addFromBase64 API? it would be great if you could share us a sample workbook, we would like to investigate whether we can unblock your scenario or not.

BTW, I have tried your code it works fine in my end.

This is the gist I created from your code. can you have a try on your side? please let me if there is any issue or not. https://gist.github.com/lumine2008/39513788f189169a9cf7c15220f94077

Raymond Lu
  • 2,178
  • 1
  • 6
  • 19
  • thanks for quick reply with sample code. I tried your code, but facing same issue (i.e. worksheets not being added without giving any error). Regarding your queries - #1 - No, I dont need to support excel online. #2 - I am just selecting a sample .xlsx (created in Excel 2019) file with single worksheet. – umesh shukla Apr 11 '20 at 17:36
  • Are you usin Script Lab to run my gist? – Raymond Lu Apr 12 '20 at 01:32
  • I am creating my add-in in VS 2017 using "Excel Web Addin" template – umesh shukla Apr 12 '20 at 04:20
  • Just now, I have also tried importing your script in Script Lab, but no luck – umesh shukla Apr 12 '20 at 04:45
  • I tried another sample found in Script Lab, that open workbook in new instance, works fine, is any criteria for existing workbook to open in current instance? – umesh shukla Apr 12 '20 at 04:49
  • "debugInfo": { "code": "ApiNotFound", "message": "The API you are trying to use could not be found. It may be available in a newer version of Excel.", "errorLocation": "WorksheetCollection.addFromBase64", "statement": "worksheets.addFromBase64(...);", "surroundingStatements": [ "var workbook=context.workbook;", "var worksheets=workbook.worksheets;", ], – umesh shukla Apr 12 '20 at 05:07
  • Got above error when running another sample "Insert external worksheets" found in Script Lab. I am using Excel 2019 (64 bit) and it is NOT Office 365 – umesh shukla Apr 12 '20 at 05:10
  • What's the version of your Excel? Would you please have a try on Excel online with my gist in Script Lab? – Raymond Lu Apr 15 '20 at 05:43
  • AddFromBase64 has been in preview since 2018, not sure which Excel build you are current working on. if this is old build, i think maybe it is not support. therefore, i would suggest to upgrade your excel. thanks – Raymond Lu Apr 16 '20 at 09:13
  • my excel version is Microsoft Exel 2019 (16.0.10363.20015) 64-bit – umesh shukla Jul 26 '20 at 03:38
  • I am also getting "ApiNotFound" error while using context.workbook.getSelectedRanges(); to get RangeArea – umesh shukla Jul 26 '20 at 03:44