Skip to content

Commit

Permalink
Improvements to Add-WordTable including precontent and postcontent.
Browse files Browse the repository at this point in the history
  • Loading branch information
guidooliveira committed Feb 8, 2016
1 parent cb1bff7 commit 26486ba
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 34 deletions.
72 changes: 38 additions & 34 deletions PSWord/AddWordTable.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management.Automation;
using Novacode;
using System.IO;
using System.Diagnostics;
using System.Reflection;


namespace PSWord
{
using System.Collections;
using System.Collections.ObjectModel;
using System.Data.Odbc;

[Cmdlet(VerbsCommon.Add, "WordTable")]
public class AddWordTable : PSCmdlet
Expand All @@ -25,7 +19,7 @@ public class AddWordTable : PSCmdlet
public PSObject[] InputObject { get; set; }

[Parameter]
public TableDesign Design { get; set; }
public TableDesign Design { get; set; } = TableDesign.TableNormal;

[Parameter]
public string PostContent { get; set; }
Expand All @@ -36,22 +30,30 @@ public class AddWordTable : PSCmdlet
[Parameter]
public SwitchParameter Show { get; set; }

private int Contagem { get; set; }
private Table documentTable { get; set; }
private DocX wordDocument { get; set; }
private int Count { get; set; }
private Table DocumentTable { get; set; }
private DocX WordDocument { get; set; }
private Paragraph PreContentparagraph { get; set; }
private Paragraph PostContentparagraph { get; set; }
private string ResolvedPath { get; set; }
protected override void BeginProcessing()
{
var resolvedPath = this.GetUnresolvedProviderPathFromPSPath(this.FilePath);
this.ResolvedPath = this.GetUnresolvedProviderPathFromPSPath(this.FilePath);

if (!File.Exists(resolvedPath))
if (!File.Exists(this.ResolvedPath))
{
this.wordDocument = DocX.Create(resolvedPath);
this.WordDocument = DocX.Create(this.ResolvedPath);
}
else
{
this.wordDocument = DocX.Load(resolvedPath);
this.WordDocument = DocX.Load(this.ResolvedPath);
}
this.Count = 0;

if (!string.IsNullOrEmpty(this.PreContent))
{
this.PreContentparagraph = this.WordDocument.InsertParagraph(this.PreContent, false);
}
this.Contagem = 0;
}

protected override void ProcessRecord()
Expand All @@ -61,30 +63,29 @@ protected override void ProcessRecord()

try
{
if (this.Contagem == 0)
if (this.Count == 0)
{
this.documentTable = this.wordDocument.InsertTable(1, header.Count());
this.documentTable.Design = this.Design;

this.Contagem++;
var Column = 0;
this.DocumentTable = this.WordDocument.InsertTable(1, header.Count());
this.DocumentTable.Design = this.Design;

var column = 0;
var row = 0;
foreach (var name in header)
{
documentTable.Rows[row].Cells[Column].Paragraphs[0].Append(name.Name);
Column++;
this.DocumentTable.Rows[row].Cells[column].Paragraphs[0].Append(name.Name);
column++;
}
this.Count++;
}

var ColumnIndex = 0;
Row newRow = documentTable.InsertRow();
var columnIndex = 0;
Row newRow = this.DocumentTable.InsertRow();

foreach (var name in header)
{
string appendData = this.InputObject[0].Properties[name.Name].Value.ToString();

newRow.Cells[ColumnIndex++].Paragraphs[0].Append(appendData);
documentTable.Rows.Add(newRow);
newRow.Cells[columnIndex++].Paragraphs[0].Append(appendData);
this.DocumentTable.Rows.Add(newRow);
}
}
catch (Exception exception)
Expand All @@ -97,9 +98,13 @@ protected override void EndProcessing()
{
try
{
using (this.wordDocument)
if (!string.IsNullOrEmpty(this.PostContent))
{
this.PostContentparagraph = this.WordDocument.InsertParagraph(this.PostContent, false);
}
using (this.WordDocument)
{
this.wordDocument.Save();
this.WordDocument.Save();
}
}
catch (Exception exception)
Expand All @@ -108,9 +113,8 @@ protected override void EndProcessing()
}
if (this.Show.IsPresent)
{
ProviderInfo providerInfo = null;
var resolvedFile = this.GetResolvedProviderPathFromPSPath(this.FilePath, out providerInfo);
Process.Start(resolvedFile[0]);

Process.Start(this.ResolvedPath);
}
}
}
Expand Down
Binary file modified PSWord/PSWord.psd1
Binary file not shown.

0 comments on commit 26486ba

Please sign in to comment.