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);
Related
How do you append text past then last line of a document in the editor using an extension?
I have an extension that either creates a new, untitled document, or it appends text to the bottom/end only of an existing document. It is the latter case that I am having trouble with. The extension does not depend on caret/cursor/selection position. I've tried both edit.insert() and edit.replace(), with various position/range values of getting past the last character, but my text addition is always placed above the last line:
Before operation (line 20 is the last line of the document):
What I get. Note the existing blank line is below the inserted text:
What I want. Note the existing blank line is above the inserted text.:
The code:
var lastLine = editor.document.lineAt(editor.document.lineCount - 1);
const replaceContent = 'Inserted Text';
editor.edit((editBuilder) => {
editBuilder.replace(lastLine.range.end, replaceContent);
});
I've found lots of SO articles for inserting/replacing text, just nothing specific to adding to the very end of an editor buffer.
// Get the text editor object
const editor = vscode.window.activeTextEditor;
// Get the current document object
const document = editor.document;
// Get the last line of the document
const lastLine = document.lineAt(document.lineCount - 1);
// Get the last line text range
const range = new vscode.Range(lastLine.range.start, lastLine.range.end);
// Append the text to the document
editor.edit((editBuilder) => {
editBuilder.insert(range.end, "\nAppended text");
});
I am new to Google Apps Script and coding in general and wasn't entirely sure how to do this. I want to create code that allows me to create a new set of Google Slides based on a Slides template using the relevant rows from a Google Sheets document.
function generateNewSlides() {
var wsID = "would insert worksheet URL ID here";
var ws = SpreadsheetApp.openById(wsID).getSheetByName("Data");
var data = ws.getRange(2, 1, ws.getLastRow()-1, 5).getValues();
>the above should get the relevant table from the sheet
data.forEach(function(info){
if(info[0]){
var firstname = info[0];
var surname = info[1];
var email = info[2];
var phone = info[3];
var image = info[4];
var presName = info[5];
>the above are columns where the different pieces of data would be taken from for the placeholders in the Slides template
var slidesTemplateID = "would insert slides template URL ID here";
var slidesTemplate = SlidesApp.openById(slidesTemplateID);
var template = slidesTemplate.getSlides();
var folderID = "would insert desired folder ID for saving in here";
>the above should get me the Slides template
template.makeCopy(presName,DriveApp.getFolderById(folderID)); **>line where error occurred**
var newPresentation = DriveApp.getFilesByName(presName).next().getUrl();
var Presentation = SlidesApp.openByUrl(newPresentation);
>the above should create a copy and then open it
var shapes = (Presentation.getShapes());
shapes.forEach(function(shape){
shape.getText().replaceAllText('{{firstname}}',firstname);
shape.getText().replaceAllText('{{surname}}',surname);
shape.getText().replaceAllText('{{email}}',email);
shape.getText().replaceAllText('{{phone}}',phone);
shape.getText().replaceAllText('{{presname}}', presName)
});
>the above should replace all the placeholder tags in the template with the row data
}
});
}
Above is the code I have so far. The worksheet I am extracting data from has columns: first name, surname, email address, phone number, image (URL), and presentation name. When I try to run it I encounter an error on line 37 where it says template.makeCopy is not a function, however I am certain .makeCopy should be able to create a copy for it, no?
My main questions are:
1) What should I change to make it work, generating a new set slides for each row in the worksheet?
2) How can I add images to it replacing placeholder tags I've added in squares (not textboxes) in the template?
Thanks in advance!
Issue 1. makeCopy:
makeCopy(name, destination) is a method of the class File, which belongs to the Drive Service, not to the Slides Service. In your code, template is a list of Slides (you retrieve it by calling the method getSlides() from a Presentation). makeCopy cannot work here.
In order to make a copy of a Presentation, you should be using the Drive Service instead. You should replace these lines:
var slidesTemplate = SlidesApp.openById(slidesTemplateID);
var template = slidesTemplate.getSlides();
With this one:
var template = DriveApp.getFileById(slidesTemplateID);
Issue 2. Iterating through all shapes:
Next, you want to iterate through all shapes in your Presentation, and replace all placeholder tags with your desired text. In order to do that, you are using Presentation.getShapes(), which cannot work, since getShapes() is not a method of Presentation, but of Slide.
You should first iterate through all Slides in the Presentation, and for each Slide, iterate through all Shapes. You should replace these lines:
var shapes = (Presentation.getShapes());
shapes.forEach(function(shape){
// Replacing text lines
});
With these ones:
Presentation.getSlides().forEach(function(slide) {
slide.getShapes().forEach(function(shape) {
// Replacing text lines
})
});
Note:
In order to retrieve the copied presentation, you are currently doing this:
template.makeCopy(presName,DriveApp.getFolderById(folderID));
var newPresentation = DriveApp.getFilesByName(presName).next().getUrl();
var Presentation = SlidesApp.openByUrl(newPresentation);
There is no need to do this, you can just retrieve the ID of the created template, and open by ID, like this:
var copiedTemplate = template.makeCopy(presName,DriveApp.getFolderById(folderID));
var Presentation = SlidesApp.openById(copiedTemplate.getId());
Reference:
Slides Service
Drive Service
I have a dynamically generated docx file.
Need write the text strictly to end of page.
With Microsoft.Interop i insert Paragraphs before text:
int kk = objDoc.ComputeStatistics(WdStatistic.wdStatisticPages, ref wMissing);
while (objDoc.ComputeStatistics(WdStatistic.wdStatisticPages, ref wMissing) != kk + 1)
{
objWord.Selection.TypeParagraph();
}
objWord.Selection.TypeBackspace();
But i can't use same code with Open XML, because pages.count calculated only by word.
Using interop impossible, because it so slowwwww.
There are 2 options of doing this in Open XML.
create Content Place holder from Microsoft Office Developer Tab at the end of your document and now you can access this Content Place Holder programatically and can place any text in it.
you can append text driectly to your word document where it will be inserted at the end of your text. In this approach you got to write all the stuff to your document first and once you are done than you can append your document the following way
//
public void WriteTextToWordDocument()
{
using(WordprocessingDocument doc = WordprocessingDocument.Open(documentPath, true))
{
MainDocumentPart mainPart = doc.MainDocumentPart;
Body body = mainPart.Document.Body;
Paragraph paragraph = new Paragraph();
Run run = new Run();
Text myText = new Text("Append this text at the end of the word document");
run.Append(myText);
paragraph.Append(run);
body.Append(paragraph);
// dont forget to save and close your document as in the following two lines
mainPart.Document.Save();
doc.Close();
}
}
I haven't tested the above code but hope it will give you an idea of dealing with word document in OpenXML.
Regards,
I am trying to draw a table in poi using XWPF component using the below code
// InputStream in= Temp.class.getResourceAsStream("Sample.docx");
XWPFDocument doc = new XWPFDocument();
XWPFTable table=doc.createTable(2,2);
// CTDecimalNumber rowSize = table.getCTTbl().getTblPr().getTblStyleRowBandSize();
// rowSize.setVal(new BigInteger("10"));
// table.getRow(1).setHeight(0);
table.getRow(0).getCell(0).setText("Row-0-Cell-0");
table.getRow(0).getCell(1).setText("Row-0-Cell-1");
table.getRow(1).getCell(0).setText("Row-1-Cell-0");
table.getRow(1).getCell(1).setText("Row-1-Cell-1");
FileOutputStream out = new FileOutputStream("simpleTable.docx");
doc.write(out);
out.close();
It draws the table properly but The height of the cells are too big and width also does not falls properly in place. I saw in a note that the table are supposed to auto fit as per the content. Tried couple of things which are as follows:
Tried setting the height as shown in the commented code above but
that did not worked.
Tried reading an existing doc as shown in
inputstream commented code. That gave an exception that could not
read a poi-ooxml file.
Tried using TblStyleRowBandSize but that
always remains null. Not sure how to create a new instance of
CTDecimalNumber or TblStyleRowBandSize
thanks in advance.
Some more insight:
When I create an empty table, and add rows and column by using create, it works fine and does not inserts the formatting character. But making an empty table results in a cell created at the begining and I am still trying to find a way to remove that first column. New code
XWPFTable table=doc.createTable();
XWPFTableRow row1 = table.createRow();
row1.createCell().setText("Row-0-Cell-0");
row1.createCell().setText("Row-0-Cell-1");
XWPFTableRow row2 = table.createRow();
row2.createCell().setText("Row-1-Cell-0");
row2.createCell().setText("Row-1-Cell-1");
I'm not sure on most of your query, but I can help with the last bit, you'll want something like:
if(! table.getTblPr().isSetTblStyleRowBandSize()) {
table.getTblPr().addNewTblStyleRowBandSize();
}
System.out.println("Was " + table.getTblPr().getTblStyleRowBandSize().getVal());
table.getTblPr().getTblStyleRowBandSize().setVal(new BigInteger("12345"));
System.out.println("Now " + table.getTblPr().getTblStyleRowBandSize().getVal());
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.