Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inline patchDocument replacement #2028

Closed
pooledge opened this issue Mar 22, 2023 · 13 comments · Fixed by #2032 or #2053
Closed

Inline patchDocument replacement #2028

pooledge opened this issue Mar 22, 2023 · 13 comments · Fixed by #2032 or #2053

Comments

@pooledge
Copy link

I'm running into a problem where a simple docx template like Hello {{name}}, how are you?, being patched with:

patchDocument(fs.readFileSync('.tmp/test.docx'), {
	patches: {
		name: {
			type: PatchType.PARAGRAPH,
			children: [new TextRun({ text: 'Max' })]
		}
	}
}).then((doc) => {
	fs.writeFileSync(fileName, doc);
});

becomes MaxHello, howare you?

Is it a known thing? simple-template.docx from demos works fine for me though, but only as long as I am not moving patchable placeholders around.

I am editing using latest MS Word on Mac.

@dolanmiu
Copy link
Owner

Can you send me the test.docx? and I will investigate

@pooledge
Copy link
Author

Here we go, thank you: https://we.tl/t-O8D2l0ikE2

@pooledge
Copy link
Author

More insights incoming. As soon as I have multiple patches i.e. multiple keys in patches{}:

TypeError: Cannot read properties of undefined (reading 'text')
    at Ba (/site/node_modules/docx/build/index.js:2:327108)
    at Ha (/site/node_modules/docx/build/index.js:2:328126)
    at i (/site/node_modules/docx/build/index.js:2:330638)
    at i.next (<anonymous>)
    at o (/site/node_modules/docx/build/index.js:2:332428)

Looks like patching only one placeholder e.g. first-name is thus malforming adjacent others: {{salutation}}{{first-name}}{{last-name}} -> Max{{{{last-name}}

@dolanmiu
Copy link
Owner

Nice observations, will look into this tonight

@pooledge
Copy link
Author

Thanks! Let me know if you need more manual testing. As far as we've played with it, it's not bound to the Word on Mac — we are reproducing the same issue with templates created on Windows, too.

@dolanmiu
Copy link
Owner

dolanmiu commented Mar 23, 2023

Created a PR to fix the issue and wrote tests around it:

#2032

I used your document for the testing

If you discover anymore issues with other scenarios, let me know

dolanmiu added a commit that referenced this issue Mar 23, 2023
@pooledge
Copy link
Author

pooledge commented Mar 24, 2023

May I reopen?

Here is the minimum docx to reproduce the error below:

As soon as I have multiple patches i.e. multiple keys in patches{}:

TypeError: Cannot read properties of undefined (reading 'text')
    at Ba (/site/node_modules/docx/build/index.js:2:327108)
    at Ha (/site/node_modules/docx/build/index.js:2:328126)
    at i (/site/node_modules/docx/build/index.js:2:330638)
    at i.next (<anonymous>)
    at o (/site/node_modules/docx/build/index.js:2:332428)

My patches:

{
	"salutation": {
		"type": "paragraph",
		"children": [
			{
				"rootKey": "w:r",
				"root": [
					{
						"rootKey": "w:rPr",
						"root": [
							{
								"rootKey": "w:rFonts",
								"root": [
									{
										"rootKey": "_attr",
										"root": {
											"ascii": "Trebuchet MS",
											"cs": "Trebuchet MS",
											"eastAsia": "Trebuchet MS",
											"hAnsi": "Trebuchet MS"
										},
										"xmlKeys": {
											"ascii": "w:ascii",
											"cs": "w:cs",
											"eastAsia": "w:eastAsia",
											"hAnsi": "w:hAnsi",
											"hint": "w:hint"
										}
									}
								]
							}
						]
					},
					{
						"rootKey": "w:t",
						"root": [
							{
								"rootKey": "_attr",
								"root": { "space": "preserve" },
								"xmlKeys": { "space": "xml:space" }
							},
							"Mr"
						]
					}
				],
				"properties": {
					"rootKey": "w:rPr",
					"root": [
						{
							"rootKey": "w:rFonts",
							"root": [
								{
									"rootKey": "_attr",
									"root": {
										"ascii": "Trebuchet MS",
										"cs": "Trebuchet MS",
										"eastAsia": "Trebuchet MS",
										"hAnsi": "Trebuchet MS"
									},
									"xmlKeys": {
										"ascii": "w:ascii",
										"cs": "w:cs",
										"eastAsia": "w:eastAsia",
										"hAnsi": "w:hAnsi",
										"hint": "w:hint"
									}
								}
							]
						}
					]
				}
			}
		]
	},
	"first-name": {
		"type": "paragraph",
		"children": [
			{
				"rootKey": "w:r",
				"root": [
					{
						"rootKey": "w:rPr",
						"root": [
							{
								"rootKey": "w:rFonts",
								"root": [
									{
										"rootKey": "_attr",
										"root": {
											"ascii": "Trebuchet MS",
											"cs": "Trebuchet MS",
											"eastAsia": "Trebuchet MS",
											"hAnsi": "Trebuchet MS"
										},
										"xmlKeys": {
											"ascii": "w:ascii",
											"cs": "w:cs",
											"eastAsia": "w:eastAsia",
											"hAnsi": "w:hAnsi",
											"hint": "w:hint"
										}
									}
								]
							}
						]
					},
					{
						"rootKey": "w:t",
						"root": [
							{
								"rootKey": "_attr",
								"root": { "space": "preserve" },
								"xmlKeys": { "space": "xml:space" }
							},
							"John"
						]
					}
				],
				"properties": {
					"rootKey": "w:rPr",
					"root": [
						{
							"rootKey": "w:rFonts",
							"root": [
								{
									"rootKey": "_attr",
									"root": {
										"ascii": "Trebuchet MS",
										"cs": "Trebuchet MS",
										"eastAsia": "Trebuchet MS",
										"hAnsi": "Trebuchet MS"
									},
									"xmlKeys": {
										"ascii": "w:ascii",
										"cs": "w:cs",
										"eastAsia": "w:eastAsia",
										"hAnsi": "w:hAnsi",
										"hint": "w:hint"
									}
								}
							]
						}
					]
				}
			}
		]
	}
}

Any of those applied separately works okay. Thank you for looking into this!

@dolanmiu dolanmiu reopened this Mar 24, 2023
@dolanmiu
Copy link
Owner

Ok, thanks for investigating, I will do investigating my side too and create a fix

@dolanmiu
Copy link
Owner

Do you patch with a JSON object, or you do patch with new TextRun(...)? etc? Because the former is not supported. Needs to be an object like TextRun

@pooledge
Copy link
Author

I am patching like this:

// lib
export const font = 'Trebuchet MS';
export const getPatches = (fields: { [key: string]: string }) => {
	const patches: { [key: string]: IPatch } = {};

	for (const field in fields) {
		patches[field] = {
			type: PatchType.PARAGRAPH,
			children: [new TextRun({ text: fields[field], font })]
		};
	}

	return patches;
};

// exec
const patches = getPatches({
	salutation: 'Mr',
	'first-name': 'John'
});

@dolanmiu
Copy link
Owner

I can't seem to reproduce the issue, I have even created a demo here to try and describe the problem, but it worked flawlessly:

https://github.com/dolanmiu/docx/blob/master/demo/88-template-document.ts

Can you send the word document? So I can invesatigate on that?

@pooledge
Copy link
Author

Did you check this file: https://github.com/dolanmiu/docx/files/11059898/test.docx ?
If yes, I guess we can close this. I went the generation path rather than patching.

dolanmiu added a commit that referenced this issue Mar 30, 2023
@dolanmiu
Copy link
Owner

Fixed in #2053

Using that example document you provided as well

dolanmiu added a commit that referenced this issue Apr 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants