OpenXml: Copy OpenXmlElement between documents - merge

I have two Word documents (WordprocessingDocument), and I want to replace the contents of an element in the first with the contents in the body of the second one.
This is what I'm doing right now:
var docA = WordprocessingDocument.Open(docAPath, true);
var docB = WordprocessingDocument.Open(docBPath, true);
var containerElement = docA.MainDocumentPart.Document.Body
.Descendants<SdtBlock>()
.FirstOrDefault(sdt => sdt.SdtProperties.Descendants<SdtAlias>().Any(alias => alias.Val == containerElementName))
.SdtContentBlock;
var elementsToCopy = docB.MainDocument.Part.Document.Body.ChildElements.Where(e => e.LocalName != "sectPr"));
containerElement.RemoveAllChildren();
containerElement.Append(elementsToCopy);
Basically I get the container (an SdtBlock) from the first document using its alias to identify it, then get all the children of the second element (removing the SectionProperties which I don't want to copy) and then try to add those to the container element.
The problem is that I'm getting this exception:
Cannot insert the OpenXmlElement "newChild" because it is part of a tree.
When I invoke the last line on that code (the Append).
Any ideas on how can I achieve what I want?

You need to clone the element to copy containerElement.Append(elementsToCopy.CloneNode(true));

The elementsToCopy is still attached to it's original tree. So you would have to remove it's parents or copy them( to keep the original intact). I think there exists a removeParent() method.

Related

Word cannot open DOCX file with a table

I am trying to run mail merge against a DOCX file using Open XML API - just replacing a <w:t> element with a table (see below). Even the simplest table created using the following code results in Word erroring out when opening the file.
If I get rid of the row (so that I only have <w:tbl> / <w:tblGrid> / <w:GridCol>). there is no error, but then I cannot have any data of course.
Can anybody see what I am doing wrong?
Table table = new Table(new TableGrid(new GridColumn() { Width = "2000"}),
new TableRow(new TableCell(new Paragraph(new Run(new Text("test")))))
);
TextNode.Parent.ReplaceChild<Text>(table, TextNode);
You cannot replace <w:t> with <w:tbl>. The table is a block-level element so you can place it in the same places where you have the paragraph (<w:p>).
In other words, you can place it as a child element of one of the following: body, comment, customXml, docPartBody, endnote, footnote, ftr, hdr, sdtContent, tc, and txbxContent.
So, try something like this:
// TextNode (Text) -> Parent (Run) -> Parent (Paragraph)
var paragraph = TextNode.Parent.Parent as Paragraph;
paragraph.Parent.ReplaceChild(table, paragraph);
EDIT:
If the parent element is <w:tc>, you should add an empty paragraph to its end:
// TextNode (Text) -> Parent (Run) -> Parent (Paragraph)
var paragraph = TextNode.Parent.Parent as Paragraph;
var parent = paragraph.Parent;
parent.ReplaceChild(table, paragraph);
if (parent is TableCell)
parent.InsertAfter(new Paragraph(), table);

How to assert whether a text is present in table

I am new to protractor and want to assert whether a newly added row contains the text which i have added.
Also i want to assert whether the particular text belongs to the header section.
I have tried to some extent but my code is failing.
Below is my code
var row=element.all(by.repeater('dataRow in displayedCollection'));
var col=row.all(by.tagName('td'));
col.each(function(item)
{
item.getText().then(function(text)
{
})
})
})
Below is the HTML code
Below is UI
Sergey is correct, this would be much easier using async/await but using your example here is how I would do it.
Assuming that the newly added row is always the last row in the table you can probably do it something like this:
const rows = element.all(by.repeater('dataRow in displayedCollection'));
const relevantRow = rows.last();
relevantRow.getText().then(test => {
expect(text).toContain(mySampletext);
});
If the new row is not always the last one, then the way to find it would be different. For example, if every time you added a row the table was sorted alphabetically, then you would need to filter the list of rows to find the one you are looking for, and then check that the text matches what you expect it to be.
You don't really need to get each td element unless you want to verify that the text in each column is correct. In that case you would probably do it similar to this:
const expectedText = ['someText', 'in order', 'by', 'columns'];
const rows = element.all(by.repeater('dataRow in displayedCollection'));
const relevantRow = rows.last();
const columns = relevantRow.$$('td');
let coumnTextMatches = true;
columns.each((col, index) => {
col.getText().then(text => {
columntextMatches = expectedText[index] === text;
});
});
expect(columntextMatches).toEqual(true);
This may or may not be completely accurate. I just did it off the top of my head without testing it out but it should be pretty close to something like that.
For the headers it would be similar.
get all the header elements
iterate over each one and check that the text matches what you expect

SAPUI5 List Item context binding, get the next path

I have a table of list items (questions) and I want to be able to re-arrange them. See screenshot.
Currently, on the button down press, I can get the current binding context and I am getting that sequence property (001). What I want to be able to do is also be able to get the path of the next list items binding context (002 in this case).
Current code...
// Move Question Down
onQuestionMoveDown: function (oEvent) {
// Get binding context
var source = oEvent.getSource().getBindingContext("view");
var path = source.getPath();
var object = source.getModel().getProperty(path);
var currentQuestionSequence = object.Sequence;
MessageToast.show("Current # " + currentQuestionSequence);
}
Then once I have that I can sort my updates logic.
A possible solution could be that you have an order value on your model and you update that order when the user clicks on the button.
If the list is sorted by that value you will achieve what you're looking for.
The items should be bound to the table via list binding, and so the data set would be an Array, the path for each line will be like ".../itemSet/0, .../itemSet/1, ...". So possible solution could be:
function getNextItem(oItem){
var oContext = oItem.getBindingContext("view"), // assumpe the model name is view
sPath = oContext.getPath(),
sSetPath = sPath.substr(0, sPath.lastIndexOf("/")),
iMaxLen = oContext.getProperty(sSetPath).length,
iCurIndex = parseInt(sPath.substr(sPath.lastIndexOf("/")+1));
// If it's already reach to be bottom, return undefined
return iCurIndex < iMaxLen -1 ? oContext.getProperty(sSetPath + "/" + ++iCurIndex) : undefined;
}
Regards,
Marvin

Iterate until matching text is found

I am writing a test which should iterate until a partial matching text is found . Once found , it should do some thing. Here is my code.
let getName = await $$('.button').getText();
if (getName === 'New name') {
// do something here
}
My test doesn't iterate even though there exists a matching name. It should also consider partials tests, for instance New name 1.
Appreciate your suggestions.
My answer is a shot in a dark due to lack of details in your question, but I'll try to guess your problem.
$$().getText() returns array of strings, thus your code must be
let names = await $$('.button').getText();
for (let i=0; i<names.length; i++) {
if (names[i].includes('New name')) {
// do something here
}
}
But when you await $$().getText(), and there are more than ~10 elements, be ready to face this error Random occurrences of Failed: ECONNREFUSED connect ECONNREFUSED 127.0.0.1 when running protractor tests
Actually, I must include another guess here based on an assumption you'll need to interact with the first element that matches condition
let $newNameElement = await $$('.button').filter( $elem =>
(await $elem.getText()).toLowerCase().includes('new name');
).get(0);
// do whatever you want with your $newNameElement

Create an Array from list of objects in MaxScript and add them to a new layer

I'm super new to Maxscript and want to automate a process, I've been looking at some tutorials, but I'm running into a issue with selection. What I'm trying to do, is I have a list of strings (that I might have to add to) that represent objects in the max file that I want to select (if they exist in that file) and then add to a new layer.
for instance:
/* I have a big long list of objects I want to mass select, this has to be hardcoded because its a similar list that exists in a ton of max files */
rObj1 = "testObj1"
rObj2 = "sampleObj2"
""
rObj99 = "newObj90"
/*I want to then add it to an array
removeList = #(rObj***)
/* Then run through each entry in the array to make sure it exists and then add it to my selection
for i in removeList do
(
if i != undefined then select (i)
)
/*Then Add what I have selected to a new layer
newLayer = LayerManager.newLayerFromName "removed_list"
for obj in selection do newLayer.addNode obj
I keep getting an error when it comes to selection, being new to Max I'm not sure what to do.
You are trying to select string where you should be selecting (or adding to the layer) an object:
newLayer = LayerManager.newLayerFromName "removed_list"
for objName in removeList where isValidNode (getNodeByName objName) do
newLayer.addNode (getNodeByName objName)