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
648 stars 92 forks source link

RichApi.Error: InvalidRequest on setting up field value #4050

Closed ishanig11 closed 2 months ago

ishanig11 commented 4 months ago

I am using following code to set field values from Ms Word document

Word.run(async (context) => { var customDocProps = context.document.properties.customProperties; context.load(customDocProps); return context.sync() .then(function () { // now load actual property var filenameProp = customDocProps.getItemOrNullObject("PW", { matchPrefix: true }); context.load(filenameProp); return context.sync() .then(function () { try{ var fields = context.document.body.fields.load("items"); fields.load(["code", "result"]); return context.sync() .then(function () { if (fields.items.length > 0) { fields.items.forEach((k,i)=> {
k.result.insertText(k.result.text, "Updated Field Value"); k.load(["code", "result"]); const paragraph = context.document.body.insertParagraph("Hello World", Word.InsertLocation.start); return context.sync().then(function () {
console.log("Updated Field"); }); }); } }); } catch (error) { console.log("Error: " + error); } }); });

await context.sync(); console.log("Items", customDocProps.items); });

But this code returns following error message: Uncaught (in promise) RichApi.Error: InvalidRequest at new n (word-web-16.00.js:25:335398) at o.processRequestExecutorResponseMessage (word-web-16.00.js:25:399844) at word-web-16.00.js:25:397907

Your Environment

This is on Windows system. Latest office js file from CDN

Expected behavior

Field value should get updated without any issue.

Current behavior

Getting Invalid request error

Steps to reproduce

  1. Call above code on any action in Word Document which has custom fields added through Quik Parts
  2. Check console logs
yilin4 commented 4 months ago

@ishanig11 Could you please provide your document and your complete gist? And I found there is some syntax errors in your code. There are some references of word js api as following. customDocProps.getItemOrNullObject https://learn.microsoft.com/en-us/javascript/api/word/word.custompropertycollection?view=word-js-preview#word-word-custompropertycollection-getitemornullobject-member(1) the second parameter of k.result.insertText https://learn.microsoft.com/en-us/javascript/api/word/word.range?view=word-js-preview#word-word-range-inserttext-member(1)

ishanig11 commented 4 months ago

FieldRefresh.docx I dont see any syntax error in above code. I call this code inside one of function. And this function gets called as ExecuteAction of office manifest command.

yilin4 commented 4 months ago

@ishanig11 Can you provide your screenshots of the error? image I tried to run your code but the code with red underline is not correct. As the link I shared you before, the customDocProps.getItemOrNullObject doesn't have the second parameter and the insertText's second parameter should be one of these(insertLocation: Word.InsertLocation | "Replace" | "Start" | "End" | "Before" | "After")

ishanig11 commented 3 months ago

Thanks for pointing this. I changed error line to k.result.text.replace(k.result.text, "New Value") But this is not replacing field value. I want to update field value which user can do in Ms Word by "Update Field".

yilin4 commented 3 months ago

@ishanig11 It should be k.result.insert(k.result.text, "Replace"), you can try it. And if there still are questions on this please let me know~ thank you.

ishanig11 commented 3 months ago

DemoDocument.docx image

Refer attached document. Here Hello Word is getting inserted. But you can see field values are not replace.

yilin4 commented 3 months ago

@ishanig11 Can you describe the scenario that you use field? From the document, I think you just want to replace the title and the major_rev to other string. If so, maybe the content control will be more suitable.

ishanig11 commented 3 months ago

[Uploading ReactWordPortFolio.zip…]() Steps.docx Refer this document for exact requirement and current issue. [Uploading ReactWordPortFolio.zip…]() Project file.

Kindly check and let me know in case of any questions.

ishanig11 commented 3 months ago

@yilin4 any update on this issue?

yilin4 commented 3 months ago

@ishanig11 Sorry for responding late. I have tried the following statement to insertText to a Field, you can have a try. But it's for changing the first Field in a document, and you can modify it according to your need.

const fields = context.document.body.fields; const field = fields.getFirstOrNullObject(); field.result.insertText("123", "Replace");

ishanig11 commented 3 months ago

@yilin4 : I tried above approach but still no change in document Quik Parts field value. Refer below screenshot: image

yilin4 commented 3 months ago

@ishanig11 Sorry, I have tried it again, and found it just works in desktop. It has been put on our backlog<Bug#8727789> for internal track. We will keep track of this issue and let you know if there are any updates.

ishanig11 commented 3 months ago

@yilin4 : We are completely blocked due to this issue? Can you please talk with developer and find some workaround for now? This is blocker issue for us.

Thanks in advance.

ishanig11 commented 3 months ago

@yilin4 : I tried above approach but it did not work in Desktop version also. And I got below error: image

ishanig11 commented 3 months ago

@yilin4 : any update? As mentioned earlier, please provide some workaround as mentioned earlier. As this is blocker issue for us. Please take it on high priority.

Thanks in advance

ishanig11 commented 3 months ago

Any update?

yilin4 commented 3 months ago

@ishanig11 Sorry to respond late. It can work on desktop, but the field's type can't be others. You can try to set the field type to addin and have a try again. About the online version, can using ContentControl meet your needs? Inserting a ContentControl with tag in the selection range. const selection = context.document.getSelection(); const cc = selection.insertContentControl(); cc.tag = "revision"; Getting the ContentControl with tag and changing its content. const ccs = context.document.body.contentControls; const target = ccs.getByTag("revision"); target.getFirstOrNullObject().insertText("123", "Replace");

ishanig11 commented 3 months ago

We cant't use ContentControl in our document as we have older documents which are using Quik Parts. So need solution for Quik Parts refresh.

yilin4 commented 3 months ago

@ishanig11 Now, the online Field Object can't support the function you need. Could you estimate the user base and the revenue from your new add-in? So, we can better escalate this case and priories it. And the current supported field type of online Word and desktop Word is like following: image

ishanig11 commented 3 months ago

@yilin4 : we have developed this feature and now will showcase these to our clients. As per our current surveys client will be interested in this feature. But right now I cant comment on user base and revenue. But definitely it will be good one.

Can you atleast tell me workaround to automatically refresh DOCPROPERTY field types in Desktop version for now?

yilin4 commented 3 months ago

@ishanig11 Can you tell me the specific field type that in your document that you want to refresh?

ishanig11 commented 3 months ago

I want to refresh docproperty fields. These are fields basically from info custom properties tab of word document.

On Thu, 22 Feb 2024, 19:28 yilin4, @.***> wrote:

@ishanig11 https://github.com/ishanig11 Can you tell me the specific field type that in your document that you want to refresh?

— Reply to this email directly, view it on GitHub https://github.com/OfficeDev/office-js/issues/4050#issuecomment-1959505230, or unsubscribe https://github.com/notifications/unsubscribe-auth/BFS576ZUZ4QNE2JVNH7ST3TYU5FIDAVCNFSM6AAAAABCJ56MBGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSNJZGUYDKMRTGA . You are receiving this because you were mentioned.Message ID: @.***>

ishanig11 commented 3 months ago

image: refer this image. I want to refresh docproperty fields. These are fields basically from info->custom properties tab of word document.

yilin4 commented 3 months ago

@ishanig11 You can try this. It can work on desktop to refresh the docProperty field. var fields = context.document.body.fields.load("items"); fields.load(["items"]); await context.sync(); for(let i = 0; i < fields.items.length; i++){ const field = fields.items[i]; field.load(); await context.sync(); if (field.type === Word.FieldType.docProperty){ field.result.insertText("test", "Replace"); } }

ishanig11 commented 3 months ago

With this field is not updated. See below image: image

yilin4 commented 3 months ago

@ishanig11 I record this, you can see it. It's the desktop version.

https://github.com/OfficeDev/office-js/assets/107384367/1384b5f7-404b-44da-8082-c1da2ecd3a94

ishanig11 commented 3 months ago

It worked for desktop. Thank you.

But need solution for online version also :) If you can provide some workaround for the same that would be really helpful.

yilin4 commented 3 months ago

@ishanig11 Now, the online version only has 4 field type, so if your document has the docProperty field, the online Word can't recognize it. If you have this need, we would like to suggest you go to https://techcommunity.microsoft.com/t5/microsoft-365-developer-platform/idb-p/Microsoft365DeveloperPlatform to ask a new question. Feature requests on it are considered when we go through our planning process.

ishanig11 commented 3 months ago

const ccs = context.document.body.contentControls; const target = ccs.getByTag("revision"); target.getFirstOrNullObject().insertText("123", "Replace");

If content control is marked as contents can not be edited then through program we can t update value. I want to make these fields as read only for users so that they can't modify but should be able to update values programaically. image

yilin4 commented 3 months ago

@ishanig11 There are two properties in the content control named cannotEdit and cannotDelete, I think you can use these two properties to control the content control not to be edited or deleted by users.

microsoft-github-policy-service[bot] commented 2 months ago

This issue has been automatically marked as stale because it is marked as needing author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. Thank you for your interest in Office Add-ins!

microsoft-github-policy-service[bot] commented 2 months ago

This issue has been automatically marked as stale because it is marked as needing author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. Thank you for your interest in Office Add-ins!

microsoft-github-policy-service[bot] commented 2 months ago

This issue has been automatically marked as stale because it is marked as needing author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. Thank you for your interest in Office Add-ins!

microsoft-github-policy-service[bot] commented 2 months ago

This issue has been closed due to inactivity. Please comment if you still need assistance and we'll re-open the issue.

microsoft-github-policy-service[bot] commented 2 months ago

This issue has been closed due to inactivity. Please comment if you still need assistance and we'll re-open the issue.