0

Following is the one of the functionality in our product a. Once user clicks on 'update' button, reads the Excel in byte format and send it to the server(java application server which saves that byte to database)

b. After getting success response, the excel is getting closed automatically. I didn't find any exception.It's happening only in desktop version, but working fine in online version

Following is function which read the excel and makes an API call to the server

function saveInput(){
    Office.context.document.getFileAsync("compressed",
    { sliceSize: 100000 },
    function (result) {
        updateStatus(result.status);
        if (result.status == Office.AsyncResultStatus.Succeeded) {

            // Get the File object from the result.
            var myFile = result.value;
            var state = {
                file: myFile,
                counter: 0,
                sliceCount: myFile.sliceCount
            };

            //updateStatus("Getting file of " + myFile.size + " bytes");
            getSlice(state);
        }
        else {
          //  updateStatus("Please re-open the add-on and save the input");
        }
    });
}

function getSlice(state) {
    state.file.getSliceAsync(state.counter, function (result) {
        if (result.status == Office.AsyncResultStatus.Succeeded) {
            //updateStatus("Sending piece " + (state.counter + 1) + " of " + state.sliceCount);
            sendSlice(result.value, state);
        }
        else {
            updateStatus(result.status);
        }
        state.file.closeAsync();
    });
}

function sendSlice(slice, state) {
    var data = slice.data;
    // If the slice contains data, create an HTTP request.
    if (data) {

        var selectedCompany = $('#company-container').val();
        var selectedScenario = $('#forecast-container').val();

        var selectedUICompany = $('#company-container').data(selectedCompany);
        var supportUIScenarios = $('#forecast-container').data(selectedScenario);

        var sessionId = $('body').data('sessionId');
        var companyId = selectedUICompany.uiCompany.dbId;
        var companyVersion = selectedUICompany.uiCompany.version;
        var companyUniqueId = selectedUICompany.uiCompany.companyId;
        var companyDivisionName = selectedUICompany.uiCompany.uiCompanyDivisions[0].name;
        var serviceProviderId =  selectedUICompany.serviceProviderId;

        var scenarioId = supportUIScenarios.docId;

        var response=$('body').data('login-response');
        var uiUser=response.uiUser;
        var userId=uiUser.dbId;
        var emailAddress = uiUser.emailAddress;

        var dataR = {
            'userId' : userId,
            'emailAddress' : 'emailAddress',
            'sessionId' : sessionId,
            'companyId' : companyId,
            'companyUniqueId' : companyUniqueId,
            'companyVersion' : companyVersion,
            'companyDivisionName' : companyDivisionName,
            'serviceProviderId' : serviceProviderId,
            'scenarioId' : scenarioId,
            'excelStream' : data
        };      
        $.ajax({
            url: "https://example.com?&operationId=UPDATE_EXCEL_INPUT",
            type: "POST",
            crossDomain: true,
            crossOrigin: true,
            contentType: "application/json",
            dataType: "json",
            data: JSON.stringify(dataR),
            headers: {
                'sessionId': sessionId
            },
            success: function (response, status, xhr) {
                var status = response.status;
                if(status != 'success')
                {
                 updateStatus(response.msg);
                }else{
                 updateStatus("Update successful");
                }
            },
            error: function (request, error) {
               updateStatus(error);
            }
        });       
    }
}

Any suggestions? Thanks in advance.

daedsidog
  • 1,732
  • 2
  • 17
  • 36
madireddy
  • 107
  • 3
  • 13
  • Looking like `state.file.closeAsync();` a very likely culprit? – Dan Rayson Nov 28 '18 at 13:47
  • if state.file.closeAsync(); is not added, the reading of excel will fail if already read as per MSDN. "No more than two documents are allowed to be in memory; otherwise the getFileAsync operation will fail. Use the File.closeAsync method to close the file when you are finished working with it." – madireddy Nov 29 '18 at 04:17
  • On successful response you're eventually calling into GetSlice, which closes it. I haven't tested it, just a hunch. – Dan Rayson Nov 29 '18 at 10:05

0 Answers0