OfficeDev / office-js

A repo and NPM package for Office.js, corresponding to a copy of what gets published to the official "evergreen" Office.js CDN, at https://appsforoffice.microsoft.com/lib/1/hosted/office.js.
https://learn.microsoft.com/javascript/api/overview
Other
685 stars 95 forks source link

insertFileFromBase64 loads the content in different result and reformat the merge blocks #4901

Open andregloeckner opened 1 month ago

andregloeckner commented 1 month ago

We create a lot of complexe document processing templates and have created a word addin which loads the files directly from our web application. It works well more or less...

But in some complexe edge cases we found problems by modified placeholders durring loading.

See: Correct version, can you achive by use Word->Document->Open with the file, or you use the follow code snipped.

OfficeExtension.config.extendedErrorLogging = true;
await Word.run(async (context) => {
    context.application.createDocument(response.content).open();
    await context.sync();
}).catch(function (error) {
    messageContext.add({
        intent: "error",
        title: CodeStrings.Messages.ErrorTitle,
        text: 'Error: ' + JSON.stringify(error)
    });
});

image

The problem happens when you try to load the byte array content directly in the Current word session:

await Word.run(async (context) => {
    context.document.insertFileFromBase64(response.data.content, Word.InsertLocation.replace, {
        importTheme: true,
        importStyles: true,
        importCustomProperties: true,
        importCustomXmlParts: true,
        importParagraphSpacing: true,
        importPageColor: true,
        importChangeTrackingMode: true,
        importDifferentOddEvenPages: true,
    });
    await context.sync();
}).catch(function (error) {
    console.log('Error: ' + JSON.stringify(error));
    if (error instanceof OfficeExtension.Error) {
        console.log('Debug info: ' + JSON.stringify(error.debugInfo));
    }
});

image

In the wrong case, you can see that the merge block is moved to a different position.

Your Environment

Expected behavior

We want to use the loading of a byte array in the same word window, the only methode I found is using the

Current behavior

Steps to reproduce

When open a docx file as byte[] to new Word with that code block, the format is fine.

await Word.run(async (context) => {
    context.application.createDocument(response.data.content).open(); 
    await context.sync();
}).catch(function (error) {
    console.log('Error: ' + JSON.stringify(error));
    if (error instanceof OfficeExtension.Error) {
        console.log('Debug info: ' + JSON.stringify(error.debugInfo));
    }
});

When open a docx file as byte[] to current Word instance with that code block, it looks wrong.

await Word.run(async (context) => {
    context.document.insertFileFromBase64(response.data.content, Word.InsertLocation.replace, {
        importTheme: true,
        importStyles: true,
        importCustomProperties: true,
        importCustomXmlParts: true,
        importParagraphSpacing: true,
        importPageColor: true,
        importChangeTrackingMode: true,
        importDifferentOddEvenPages: true,
    });
    await context.sync();
}).catch(function (error) {
    console.log('Error: ' + JSON.stringify(error));
    if (error instanceof OfficeExtension.Error) {
        console.log('Debug info: ' + JSON.stringify(error.debugInfo));
    }
});

Link to example(s)

Sample file well formated Extract.docx

Sample file after open with insertFileFromBase64() Extract - broken.docx

Piyush-IT commented 1 month ago

Facing same issue

shanshanzheng-dev commented 1 month ago

Hi @andregloeckner @Piyush-IT Thanks for reporting this issue. Seem I can repro, we'll take a look and report back if we have a suggestion for you.

andregloeckner commented 1 month ago

Hi @shanshanzheng-dev,

do you already have an idea how to fix that? We cannot proced with the plugin idea we have, because with open we loose the context of which file it is. The context contain informations like, DocumentName, DocumentId, TemplateEntity, CurrentRevision and so on. Serializable to JSON.

So we have just the possiblity to open in current Word with staying in the document context we have. Or Call open with send an embedded json with the byte array to the new Word.

Best regards

Searion commented 1 month ago

@andregloeckner Thanks for reporting this. We have put on our backlog<Bug#9350173> for internal track. We found it happens for all insertfilefrombase64 apis and are still working on it. We will let you know if there are any updates.

Also, since this example is quite complex, I want to make sure we haven't missed anything. One content control should have included two rows but wrongly included all the table after inserting. Have you encountered similar problems with contentcontrols other than tables?

andregloeckner commented 1 month ago

@Searion I asked my collegues about your questions and it looks like that all similar problems we encountered were related to the tables. Greetings