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

Extract all the variables from the word document #90

Closed
cesarve77 opened this issue Oct 24, 2019 · 12 comments
Closed

Extract all the variables from the word document #90

cesarve77 opened this issue Oct 24, 2019 · 12 comments

Comments

@cesarve77
Copy link

cesarve77 commented Oct 24, 2019

First thanks and congratulation for this great package

What is the best way to extract all the variables (in dot notation for example) from the word document.

My idea is build the query based in the variables asked in the docx.

Thanks in advance

@mathe42
Copy link
Contributor

mathe42 commented Jan 29, 2020

Interesting question... Maybe use https://www.npmjs.com/package/textract anderen then search with regex

@wildhart
Copy link

wildhart commented Feb 7, 2020

I've come up with an inefficient solution to this, but imo there should be an easier way.

Since the data object can be a promise-returning function, I would expect this to work:

report = await createReport({
	template,
	cmdDelimiter: ['[', ']'],
	data: function(query) {
		console.log('field:', query);
		return new Promise((resolve, reject) => resolve('hello'));
	},
});

But in the data function query is always undefined. My templates have fields like [ProjectNumber]

Why doesn't this work?

Here's my solution which does work, by catching the error in a loop:

const data = {};
let report = null;
do {
	try {
		report = await createReport({
			template,
			cmdDelimiter: ['[', ']'],
			data,
			// USE ONLY IN THE BROWSER, AND WITH TRUSTED TEMPLATES
			noSandbox: true, // WARNING: INSECURE
		});
		import('file-saver').then(FileSaver => FileSaver.saveAs(new Blob([report]), res.name+'.done.docx'));
	} catch(e) {
		const m = e.toString().match(/INS (\w+)/);
		if (m) {
			data[m[1]] = m[1];
			console.log('new field:', m[1]);
		}
		else throw e;
	}
} while (!report);
console.log(Object.keys(data)); // this is a list of the fields!

PS - @cesarve77 please can you edit the title of this issue to something like [question] get list of fields so people have more chance of finding it.

@mathe42
Copy link
Contributor

mathe42 commented Feb 7, 2020

Maybe you also can use a Proxy object in a first run. And getting the requestet fields...

@wildhart
Copy link

wildhart commented Feb 7, 2020

I tried a Proxy object but that didn't work. I think internally docx-templates is using something like Object.keys(data) to get a list of fields in the data object because it doesn't even try proxy.get() on fields which don't already exist.

@wildhart
Copy link

wildhart commented Feb 7, 2020

FYI, the alternative module docxtemplater handles this really easily with its nullgetter setting: https://docxtemplater.readthedocs.io/en/latest/configuration.html#nullgetter in which part.value is the field name.

@bulolo
Copy link

bulolo commented Apr 13, 2020

i also need to extract all the variables from template which has follow the +++=XXX+++ by template-docx'way
is there any good way to parse it self.

@jjhbw jjhbw changed the title QUESTION Extract all the variables from the word document Apr 13, 2020
@jjhbw
Copy link
Collaborator

jjhbw commented Apr 13, 2020

I was planning on implementing something like this. I'll let you know when i've gotten around to it.

@bulolo
Copy link

bulolo commented Jun 4, 2020

any progress?

@bernardohmr
Copy link

I hope someone shows up with this solution soon also (=

jjhbw added a commit that referenced this issue Jun 23, 2020
jjhbw added a commit that referenced this issue Jun 23, 2020
jjhbw added a commit that referenced this issue Jun 23, 2020
@jjhbw
Copy link
Collaborator

jjhbw commented Jun 23, 2020

Added this feature in https://github.com/guigrpa/docx-templates/releases/tag/v4.3.0

@jjhbw jjhbw closed this as completed Jun 23, 2020
@Arend-dev
Copy link

Arend-dev commented Sep 10, 2020

Hi there,

When trying to use listCommands, I always get an error:

"...\node_modules\jszip\lib\utils.js:454
new Error("Can't read the data of '" + name + "'. Is it " +
^

Error: Can't read the data of 'the loaded zip file'. Is it in a supported JavaScript type (String, Blob, ArrayBuffer, etc) ?"

Do not know what I am doing wrong. I start node with "--unhandled-rejections=strict ".

@jjhbw
Copy link
Collaborator

jjhbw commented Sep 10, 2020

@Arend-dev Please make a new issue for stuff like this.

I can't do much without seeing your code. First off, can you verify that you are using it correctly by checking the suggestion in the error message (see below)?
...Is it in a supported JavaScript type (String, Blob, ArrayBuffer, etc) ?

The unhandled rejections flag should have little to do with it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants