How to delete an inserted OOXML comment using Word JS API? - javascript

I am trying to insert comments inside word document using insertOoxml method. The comment gets inserted successfully.
I want to delete this manually-inserted comment based on one of the user's actions. For example, when they switch from one feature of my add-in to another. I am trying to delete the comment parts from the Ooxml string using regex match and replace for this to work.
Word.run(async(context) => {
let body = context.document.body
let bodyOOXML = body.getOoxml()
await context.sync()
let bodyOOXMLText = bodyOOXML.value
bodyOOXMLText = bodyOOXMLText.replace(/<relationship(.*?)target="comments.xml(.*?)comments">/g, '')
bodyOOXMLText = bodyOOXMLText.replace(/<w:commentRangeStart(.*?)w:commentRangeEnd(.*?)\/>/g, '')
bodyOOXMLText = bodyOOXMLText.replace(/<w:comments(.*?)w:comments>/g, '')
bodyOOXMLText = bodyOOXMLText.replace(/<pkg:part(.*?)comments\+xml(.*?)word\/comments\.xml">(.*?)<\/pkg:part>/g, '')
body.insertOoxml(bodyOOXMLText, Word.InsertLocation.replace)
await context.sync()
})
It throws a GeneralException error. I think I'm corrupting the XML object somewhere, so just wanted to confirm
a. Is this a right approach/workaround to my problem?
b. What am I missing to replace here?
c. Is there any other sophisticated solution possible?

The regex method is not safe. The approach is still the same. Use XML parser and delete the relevant nodes from the XML DOM tree.
Code sample:
export function removeCommentsFromXML(xmlString){
let xmlText = ''
try{
// initialize DOM parser
let parser = new DOMParser()
let namespace = []
// parse XML string into XML DOM object
let xmlDoc = parser.parseFromString(xmlString, 'text/xml')
// get xml namespace prefix for 'pkg'
namespace['pkg'] = xmlDoc.documentElement.getAttribute('xmlns:pkg')
// get all 'pkg:part' nodes
let allChildrenNodes = xmlDoc.getElementsByTagNameNS(namespace['pkg'], 'part')
// delete comments.xml node in pkg:part
let currentChildNode = allChildrenNodes[0]
while (currentChildNode!==null && currentChildNode.getAttribute('pkg:name').match('comments.xml')===null) {
currentChildNode = currentChildNode.nextSibling
}
if(currentChildNode!==null) currentChildNode.parentNode.removeChild(currentChildNode)
// get document relationship package
currentChildNode = allChildrenNodes[0]
while (currentChildNode!==null && currentChildNode.getAttribute('pkg:name').match('word/_rels')===null) {
currentChildNode = currentChildNode.nextSibling
}
// get all relationships
let relationships = currentChildNode.getElementsByTagName('Relationship')
// delete comment relationship from relationships
let currentRelationship = relationships[0]
while (currentRelationship!==null && currentRelationship.getAttribute('Target').match('comments.xml')===null) {
currentRelationship = currentRelationship.nextSibling
}
if(currentRelationship!==null) currentRelationship.parentNode.removeChild(currentRelationship)
// get main document
currentChildNode = allChildrenNodes[0]
while (currentChildNode!==null && currentChildNode.getAttribute('pkg:name').match('/word/document.xml')===null) {
currentChildNode = currentChildNode.nextSibling
}
// get w namespace
namespace['w'] = currentChildNode.childNodes[0].childNodes[0].getAttribute('xmlns:w')
// get commentRangeStart nodes
let commentRangeStartNodes = currentChildNode.getElementsByTagNameNS(namespace['w'], 'commentRangeStart')
while(commentRangeStartNodes.length>0) {
commentRangeStartNodes[0].parentNode.removeChild(commentRangeStartNodes[0])
}
// get commentReference nodes
let commentReferenceNodes = currentChildNode.getElementsByTagNameNS(namespace['w'], 'commentReference')
while(commentReferenceNodes.length>0) {
commentReferenceNodes[0].parentNode.removeChild(commentReferenceNodes[0])
}
// get commentRangeEnd nodes
let commentRangeEndNodes = currentChildNode.getElementsByTagNameNS(namespace['w'], 'commentRangeEnd')
while(commentRangeEndNodes.length>0) {
commentRangeEndNodes[0].parentNode.removeChild(commentRangeEndNodes[0])
}
xmlText = new XMLSerializer().serializeToString(xmlDoc)
}
catch(err){
console.log(err)
}
return xmlText
}
the modified XML string can now be inserted using
body.insertOoxml(xmlText, Word.InsertLocation.replace)

Related

Async JS validation issues for html textarea

I'm trying to replicate the code in this article:
https://depth-first.com/articles/2020/08/24/smiles-validation-in-the-browser/
What I'm trying to do different is that I'm using a textarea instead of input to take multi-line input. In addition to displaying an error message, I also want to display the entry which doesn't pass the validation.
The original validation script is this:
const path = '/target/wasm32-unknown-unknown/release/smival.wasm';
const read_smiles = instance => {
return smiles => {
const encoder = new TextEncoder();
const encoded = encoder.encode(`${smiles}\0`);
const length = encoded.length;
const pString = instance.exports.alloc(length);
const view = new Uint8Array(
instance.exports.memory.buffer, pString, length
);
view.set(encoded);
return instance.exports.read_smiles(pString);
};
};
const watch = instance => {
const read = read_smiles(instance);
document.querySelector('input').addEventListener('input', e => {
const { target } = e;
if (read(target.value) === 0) {
target.classList.remove('invalid');
} else {
target.classList.add('invalid');
}
});
}
(async () => {
const response = await fetch(path);
const bytes = await response.arrayBuffer();
const wasm = await WebAssembly.instantiate(bytes, { });
watch(wasm.instance);
})();
For working with a textarea, I've changed the watch function to this and added a <p id="indicator"> element to the html to display an error:
const watch = instance => {
const read = read_smiles(instance);
document.querySelector("textarea").addEventListener('input', e => {
const { target } = e;
var lines_array = target.value.split('/n');
var p = document.getElementById("indicator");
p.style.display = "block";
p.innerHTML = "The size of the input is : " + lines_array.length;
if (read(target.value) === 0) {
target.classList.remove('invalid');
} else {
target.classList.add('invalid');
}
});
}
I'm not even able to get a count of entries that fail the validation. I believe this is async js and I'm just a beginner in JavaScript so it's hard to follow what is happening here, especially the part where the function e is referencing itself.
document.querySelector("textarea").addEventListener('input', e => {
const { target } = e;
Can someone please help me in understanding this complicated code and figuring out how to get a count of entries that fail the validation and also printing the string/index of the same for helping the user?
There is a mistake in you code to count entries in the textarea:
var lines_array = target.value.split('\n'); // replace /n with \n
You are asking about the function e is referencing itself:
The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables. You can find more informations Mdn web docs - Destructuring object

Reference/set nested XML object property using path in a string - JavaScript

This is not for web development. I am using ES3.
How do I get the information from the xml element proof using javascript in this scenario?
My way of looking for the proof element with xml[xmlVariable] doesn't work - it returns nothing. But when you enter xml.ait.pages.proof in the console (while the program is held by breakpoint at the return expression) it returns the "desired info" from the proof element correctly.
I've read up on dot/bracket notation thinking that would be the solution but nope.
What's the correct syntax here?
<root>
<ait>
<pages>
<proof>desired info</proof>
</pages>
</ait>
</root>
var xmlFile = "C:\Users\user\Desktop\info.xml"
var xmlElementPath = "ait.pages.proof"
var info = readXMLVar(xmlElementPath, xmlFile)
function readXMLVar(xmlVariable, xmlFilePath) {
var file = new File(xmlFilePath)
file.open("r")
var content = file.read()
file.close()
var xml = new XML(content)
return xml[xmlVariable]
}
For XML I would probably query using XPath. The code you're using, however, seems to create an object structure from the parsed XML, and you then want to ask for a part of that structure using a path to it, as it were.
You can use square bracket notation as you tried, but you have to do it one property/node-level at a time. JS doesn't parse the dot separated path you provided to walk into the nested structure.
As such, you need something that can break apart the path you want, and recursively walk down the structure node by node.
Here is a basic function that can walk an object structure:
var getNodeFromPath = function (data, path, separator) {
var node_name,
node,
ret;
if (!Array.isArray(path)) {
path = path.split(separator || '.');
}
node_name = path.shift();
node = data[node_name];
if (node === undefined) {
ret = null;
} else {
if (path.length) {
ret = getNodeFromPath(node, path);
} else {
ret = node;
}
}
return ret;
};
You could call it like so:
var proof_element = getNodeFromPath(yourParsedXmlData, 'ait.pages.proof');
Note that the function I gave you has minimal control in it. You'll probably want to add some checking to make it more resistant to arbitrary input data/path problems.
Applied fixes to JAAulde's answer and some slight modifications to fit into my function. Here is my code to get and set XML variables.
!(Object.prototype.toString.call(path) === '[object Array]') is used in place of !Array.isArray(path) because I'm forced to use ES3.
function readXMLFile(xmlFilePath) {
var file = new File(xmlFilePath)
file.open("r")
var content = file.read()
file.close()
return [file, new XML(content)]
}
function getXMLVar(xmlFilePath, nodePath, separator) {
var xml = readXMLFile(xmlFilePath)[1]
// navigate xml to return target node info
var getNodeFromPath = function(data, path, separator) {
var node_name,
node,
ret
if(!(Object.prototype.toString.call(path) === '[object Array]')) {
path = path.split(separator || '.')
}
node_name = path.shift()
node = data[node_name]
if(node === undefined) {
ret = null
} else {
if(path.length) {
ret = getNodeFromPath(node, path, separator)
} else {
ret = node
}
}
return ret
}
return getNodeFromPath(xml, nodePath, separator)
}
function setXMLVar(xmlFilePath, nodePath, separator, value) {
var read = readXMLFile(xmlFilePath)
var file = read[0]
var xml = read[1]
setNodeFromPath = function(data, path, separator, value) {
var node_name,
node
if(!(Object.prototype.toString.call(path) === '[object Array]')) {
path = path.split(separator || '.')
}
node_name = path.shift()
node = data[node_name]
if(path.length > 1) {
setNodeFromPath(node, path, separator, value)
} else {
node[path[0]] = value
}
}
setNodeFromPath(xml, nodePath, separator, value)
file.open("w")
file.write(xml)
file.close()
}

Return list of Objects with Node js

I recently started development of a Node js application and it uses Selenium in a controller to fetch list of items from a web page and I want to return the fetched list of items as a JSON response.
exports.read_all_products = function (req, res) {
var driver = new webdriver.Builder().forBrowser('phantomjs').build();
driver.get('https://www.test.com/products?PC=' +req.params.category);
driver.wait(until.elementLocated(By.className('product-slide-all')), 20000, 'Could not locate the element within the time specified');
driver.findElements(By.className("product-slide-all")).then(function (elements) {
var arr = [];
elements.forEach(function (element) {
element.getAttribute("innerHTML").then(function (html) {
const dom = new JSDOM(html);
var obj = new Object();
obj.product_name = dom.window.document.querySelector(".product-name").textContent;
obj.product_code = dom.window.document.querySelector(".product-code").textContent;
obj.price = dom.window.document.querySelector(".product-price").textContent;
arr.push(obj);
});
});
res.json(arr);
});
}
Issue is I am always getting an empty JSON response even though items were added to the array. I want to know the proper way of handling this scenario.
Thanks.
It looks like the issue is because Selenium is running an async process, thus the response immediately returns because there is nothing blocking it.
findElements returns a Promise which you need to return the response from.
Take a look at How do I return the response from an asynchronous call?
Finally I was able to get it work with the help of webdriver.promise.map.
Moved web driver HTML extraction to separate function.
var findItems = function (category) {
var driver = new webdriver.Builder().forBrowser('phantomjs').build();
var map = webdriver.promise.map;
driver.get('https://www.test.com?PC=' + category);
driver.wait(until.elementLocated(By.className('product-slide-all')), 30000, 'Could not locate the element within the time specified');
var elems = driver.findElements(By.className("product-slide-all"));
return map(elems, elem => elem.getAttribute("innerHTML")).then(titles => {
return titles;
});
}
then call it from response handling function like bellow,
exports.read_all_products = function (req, res) {
findItems(req.params.category).then(function (html) {
var value;
var arr = [];
Object.keys(html).forEach(function (key) {
value = html[key];
const dom = new JSDOM(value);
var obj = new Object();
obj.product_name = dom.window.document.querySelector(".product-name").textContent;
obj.product_code = dom.window.document.querySelector(".product-code").textContent;
obj.price = dom.window.document.querySelector(".product-price").textContent;
arr.push(obj);
});
res.json(arr);
})
};
it's described in this stack overflow answers.

How to clone document to another collection? [duplicate]

My approach would be to get the document instance, and create a new one from the instance fields. I am sure there is a better way to do it.
You need to reset d1.isNew = true; as in:
Model.findById(yourid).exec(
function(err, doc) {
doc._id = mongoose.Types.ObjectId();
doc.isNew = true; //<--------------------IMPORTANT
doc.save(callback);
}
);
Can you clarify what you mean by "copy/clone"? Are you going trying to create a duplicate document in the database? Or are you just trying to have two vars in your program that have duplicate data?
If you just do:
Model.findById(yourid).exec(
function(err, doc) {
var x = doc;
Model.findById(yourid).exec(
function(err, doc2) {
var y = doc2;
// right now, x.name and y.name are the same
x.name = "name_x";
y.name = "name_y";
console.log(x.name); // prints "name_x"
console.log(y.name); // prints "name_y"
});
});
In this case, x and y will be two "copies" of the same document within your program.
Alternatively, if you wanted to insert a new copy of the doc into the database (though with a different _id I assume), that would look like this:
Model.findById(yourid).exec(
function(err, doc) {
var d1 = doc;
d1._id = /* set a new _id here */;
d1.isNew = true;
d1.save(callback);
}
);
Or if you're doing this from the outset, aka you created some document d1, you can just call save twice without setting the _id:
var d1 = new Model({ name: "John Doe", age: 54 });
d1.save(callback);
d1.save(callback);
There will now be two documents with differing _id's and all other fields identical in your database.
Does this clarify things a bit?
My two cents:
const doc = await DocModel.findById(id);
let obj = doc.toObject();
delete obj._id;
const docClone = new DocModel(obj);
await docClone.save();
So, a lot of these answers will work well for simple docs, but there could be an error case when you're trying to make a deep clone of complex documents.
If you have arrays of subdocs, for example, you can end up with duplicate _ids in your copied document, which can cause subtle bugs later.
To do a deep clone of a mongoose doc, I suggest trying something like:
//recursively remove _id fields
function cleanId(obj) {
if (Array.isArray(obj))
obj.forEach(cleanId);
else {
delete obj['_id'];
for (let key in obj)
if (typeof obj[key] == 'object')
cleanId(obj[key]);
}
}
let some_doc = await SomeModel.findOne({_id: some_id});
let new_doc_object = cleanId(some_doc.toObject());
let new_doc = new SomeModel(new_doc_object);
await new_doc.save();
This is going to be a pretty safe approach, and will ensure that every part of your object is cloned properly with newly generated _id fields on save.
The following code to clone documents:
Model.findById(yourid).exec(
function(err, doc) {
var newdoc = new Model(doc);
newdoc._id = mongoose.Types.ObjectId();
newdoc.save(callback);
}
);
For simply clone use this :
Context.findOne({
_id: context._id
})
.then(function(c) {
c._id = undefined;
c.name = context.name;
c.address = context.address;
c.created = Date.now();
return Context.create(c.toObject());
}).then(function(c) {
return res.json({
success: true,
context: context
});
}).catch(function(err) {
next(err, req, res);
});
const cloneDoc = (doc, model)=>{
const copyDoc = new Model({
...doc.toObject(),
_id: undefined,
});
copyDoc.isNew = true;
return copyDoc;
}
To copy a document into the same collection or different collection first get (query) the data and make a copy of the data. Afterwards remove the _id from the new list as you can't from the current data. This will allow you to insert a new record with new _id assigned from mongodb
change searchBy to what you are trying to find the document by.
change collectionA and collectionB to the name of the collection to create you copy to. Currently we are searching in collectionA and copying the data in collectionB
collectionA.find(searchBy).exec(function (err, doc) {
// create a new copy
let newDoc = { ...doc[0] }._doc;
// delete property from new copy (remove _id).
delete newDoc._id;
// insert copy into db
var newdoc = new collectionB(newDoc);
newdoc.save();
});
You can basically use .clone() to get a copy.
const cars = Cars.find();
const carsCopy = cars.clone();
await cars;
await carsCopy;
https://mongoosejs.com/docs/api.html#schema_Schema-clone

Get attribute value from xml by checking conditions

Following is my xml from where I have to get attribute value:
<R a="1" b="2">
<I attribute1="" attribute2="some text"/>
<I attribute1="" attribute2="some text"/>
<I attribute1="0" attribute2="some text"/>
<I attribute1="0" attribute2="some text"/>
</R>
Here I've to check if attribute1 is not null then I've to get value of attribute2 from I tag.How to do this???
Please help...
UPDATE:
Here's a full X-browser working script that should do the trick. Again, replace the getAttribute('attribute1') by either arguments or return the DOM and take care of the rest. This code might look a bit complicated (it uses closures to be as lightweight as possible) but it should be quite sturdy and safe to use... as long as you don't declare another function called parseXML and you don't call this parseXML prior to it being declared.
var parseXML = (function(w,undefined)
{
'use strict';
var parser,i,ie,parsed;
ie = false;
switch (true)
{
case w.DOMParser !== undefined:
parser = new w.DOMParser();
break;
case new w.ActiveXObject("Microsoft.XMLDOM") !== undefined:
parser = new w.ActiveXObject("Microsoft.XMLDOM");
parser.async = false;
ie = true;
break;
default :
throw new Error('No parser found');
}
return function(xmlString,getTags)
{
var tags,keep = [];
if (ie === true)
{
parser.loadXML(xmlString);
parsed = parser;
}
else
{
parsed = parser.parseFromString(xmlString,'text/xml');
}
tags = parsed.getElementsByTagName(getTags);
for(i=0;i<tags.length;i++)
{
if (tags[i].getAttribute('attribute1') && tags[i].getAttribute('attribute2'))
{
keep.push(tags[i].getAttribute('attribute2'));
}
}
//optional:
keep.push(parsed);//last element of array is the full DOM
return keep;
}
})(this);
var parseResult = parseXML('<r><i attribute1="" attribute2="Ignore This"/><i attribute1="foo" attribute2="got this"/></r>','i');
alert(parseResult[0] || 'nothing');//alerts 'got this' in IE and others
You can parse the XML:
var parser = new DOMParser();
var parsed = parser.parseFromString('<r a="1" b="2"><i v="" x="some text"/><i v="0" x="some important text"/></r>','text/xml');
var iTag = parsed.getElementsByTagName('i');
for (var i=0;i<iTag.length;i++)
{
if (iTag[i].getAttribute('v'))
{
console.log(iTag[i].getAttribute('x'));//do whatever
}
}
This snippet will log some important text, and not some text. That's all there is to it. If you need to store the x values, or return them just declare another variable:
var keep = [];//an array
//change console.log line by:
keep.push(iTag[i].getAttribute('x'));
This is assuming an x property will be set, if that's not always the case, an additional check can easily fix that. The full code will then look like:
function parseXML(xml)
{
'use strict';
var parser,keep,parsed,i,iTag;
parser = new DOMParser();
keep = [];
parsed = parser.parseFromString(xml,'text/xml');//xml is the string
iTag = parsed.getElementsByTagName('i');
for (i=0;i<iTag.length;i++)
{
if (iTag[i].getAttribute('v') && iTag[i].getAttribute('x'))
{
keep.push(iTag[i].getAttribute('x'));
}
}
return keep;//return array
}

Categories

Resources