guigrpa / docx-templates

Template-based docx report creation
MIT License
905 stars 146 forks source link

listCommands doesn't work with ALIAS resolution #178

Open gitowiec opened 3 years ago

gitowiec commented 3 years ago

I get

Error: Unknown alias: *ch1
    at new InvalidCommandError (node_modules/docx-templates/lib/errors.js:42:28)
    at Object.getCommand (node_modules/docx-templates/lib/processTemplate.js:563:19)
    at Object.<anonymous> (node_modules/docx-templates/lib/main.js:313:57)
    at step (node_modules/docx-templates/lib/main.js:33:23)
    at Object.next (node_modules/docx-templates/lib/main.js:14:53)
    at /home/user/workspace/project/packages/web-server/node_modules/docx-templates/lib/main.js:8:71
    at new Promise (<anonymous>)
    at __awaiter (node_modules/docx-templates/lib/main.js:4:12)
    at /home/user/workspace/project/packages/web-server/node_modules/docx-templates/lib/main.js:310:135
    at /home/user/workspace/project/packages/web-server/node_modules/docx-templates/lib/processTemplate.js:401:38
    at step (node_modules/docx-templates/lib/processTemplate.js:33:23)
    at Object.next (node_modules/docx-templates/lib/processTemplate.js:14:53)
    at /home/user/workspace/project/packages/web-server/node_modules/docx-templates/lib/processTemplate.js:8:71
    at new Promise (<anonymous>)
    at __awaiter (node_modules/docx-templates/lib/processTemplate.js:4:12)
    at processText (node_modules/docx-templates/lib/processTemplate.js:370:66)
    at Object.<anonymous> (node_modules/docx-templates/lib/processTemplate.js:325:42)
    at step (node_modules/docx-templates/lib/processTemplate.js:33:23)
    at Object.next (node_modules/docx-templates/lib/processTemplate.js:14:53)
    at fulfilled (node_modules/docx-templates/lib/processTemplate.js:5:58)

Here is how my Word template looks:

image

When I remove asterisks from aliases (change +++*ch1+++ to +++ch1+++) I get this result:

{ commands:
   [ { raw: 'INS city', type: 'INS', code: 'city' },
     { raw: 'INS date', type: 'INS', code: 'date' },
     { raw: 'INS unitName', type: 'INS', code: 'unitName' },
     { raw: 'INS address', type: 'INS', code: 'address' },
     { raw: 'INS nip1', type: 'INS', code: 'nip1' },
     { raw: 'INS nip2', type: 'INS', code: 'nip2' },
     { raw: 'INS nip3', type: 'INS', code: 'nip3' },
     { raw: 'INS nip4', type: 'INS', code: 'nip4' },
     { raw: 'FOR representative IN representatives',
       type: 'FOR',
       code: 'representative IN representatives' },
     { raw: 'INS $representative.name1 – $representative.name2',
       type: 'INS',
       code: '$representative.name1 – $representative.name2' },
     { raw: 'END-FOR representatives',
       type: 'END-FOR',
       code: 'representatives' },
     { raw: 'ALIAS ch1 INS paymentsFrequency.monthly',
       type: 'ALIAS',
       code: 'ch1 INS paymentsFrequency.monthly' },
     { raw: 'ALIAS ch2 INS paymentsFrequency.quarterly',
       type: 'ALIAS',
       code: 'ch2 INS paymentsFrequency.quarterly' },
     { raw: 'ALIAS chosenText INS paymentsFrequency.chosenText',
       type: 'ALIAS',
       code: 'chosenText INS paymentsFrequency.chosenText' },
     { raw: 'ALIAS taxYear INS paymentsFrequency.taxYear',
       type: 'ALIAS',
       code: 'taxYear INS paymentsFrequency.taxYear' },
     { raw: 'INS taxYear', type: 'INS', code: 'taxYear' },
     { raw: 'INS chosenText', type: 'INS', code: 'chosenText' },
     { raw: 'INS ch1', type: 'INS', code: 'ch1' },
     { raw: 'INS ch2', type: 'INS', code: 'ch2' },
     { raw: 'ALIAS chosenText INS vatTaxpayerRegistration.chosenText',
       type: 'ALIAS',
       code: 'chosenText INS vatTaxpayerRegistration.chosenText' },
     { raw:
        'ALIAS ch1 INS vatTaxpayerRegistration.nonRegisteredTaxpayer',
       type: 'ALIAS',
       code: 'ch1 INS vatTaxpayerRegistration.nonRegisteredTaxpayer' },
     { raw:
        'ALIAS ch2 INS vatTaxpayerRegistration.registeredTaxpayerActive',
       type: 'ALIAS',
       code: 'ch2 INS vatTaxpayerRegistration.registeredTaxpayerActive' },
     { raw:
        'ALIAS ch3 INS vatTaxpayerRegistration.registeredTaxpayerExempt',
       type: 'ALIAS',
       code: 'ch3 INS vatTaxpayerRegistration.registeredTaxpayerExempt' },
     { raw: 'INS chosenText', type: 'INS', code: 'chosenText' },
     { raw: 'INS ch1', type: 'INS', code: 'ch1' },
     { raw: 'INS ch2', type: 'INS', code: 'ch2' },
     { raw: 'INS ch3', type: 'INS', code: 'ch3' },
     { raw: 'ALIAS chosenText INS vatSettlementPeriod.chosenText',
       type: 'ALIAS',
       code: 'chosenText INS vatSettlementPeriod.chosenText' },
     { raw: 'ALIAS ch1 INS vatSettlementPeriod.monthly',
       type: 'ALIAS',
       code: 'ch1 INS vatSettlementPeriod.monthly' },
     { raw: 'ALIAS ch2 INS vatSettlementPeriod.quarterly',
       type: 'ALIAS',
       code: 'ch2 INS vatSettlementPeriod.quarterly' },
     { raw: 'INS chosenText', type: 'INS', code: 'chosenText' },
     { raw: 'INS ch1', type: 'INS', code: 'ch1' },
     { raw: 'INS ch2', type: 'INS', code: 'ch2' },
     { raw: 'ALIAS ch1 INS vatEuTaxpayerRegistration.choosen',
       type: 'ALIAS',
       code: 'ch1 INS vatEuTaxpayerRegistration.choosen' },
     { raw: 'ALIAS ch2 INS vatEuTaxpayerRegistration.none',
       type: 'ALIAS',
       code: 'ch2 INS vatEuTaxpayerRegistration.none' },
     { raw: 'INS ch1', type: 'INS', code: 'ch1' },
     { raw: 'INS ch2', type: 'INS', code: 'ch2' } ] }
jjhbw commented 3 years ago

Interesting, can you share your template? Or at least a minimal docx file I can repro this with.

gitowiec commented 3 years ago

Here is my template, please debug it

Prawne-oswiadczenie-PDOP-VAT.docx

PS: I forgot to bring back asterisks, you have to add them on your own @jjhbw before every ch1, ch2, ch3 (seems obvious)

jjhbw commented 3 years ago

Your template seems correct. Its probably an issue with listCommands not recognizing ALIAS referencing commands (*).

On Fri, 18 Dec 2020 at 13:11, Marek notifications@github.com wrote:

Here is my template, please debug it

Prawne-oswiadczenie-PDOP-VAT.docx https://github.com/guigrpa/docx-templates/files/5715532/Prawne-oswiadczenie-PDOP-VAT.docx

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/guigrpa/docx-templates/issues/178#issuecomment-748052657, or unsubscribe https://github.com/notifications/unsubscribe-auth/AC37OQHWCXGDCXPVNPPYODTSVNBIJANCNFSM4VBCIFKA .

jjhbw commented 3 years ago

I would appreciate it if you could report that bug. Note that you're of course free to ditch this library, thats the beauty of open source.