Macro Tutorial - Creating Tables

December 29, 2014

One of the Onetastic users asked about how one can insert a simple table as an example with 3 columns, so today's tutorial will do exactly that. It is pretty simple. You need to use the Insert statement to add a Table to an Outline object. You can either use an existing outline in the page (if there are any) or create one by inserting an Outline to the page. Following macro will insert an Outline to the page:

ExpandFor first Page in CurrentPage

Insert Outline at the end

To use an existing outline, one can just query for an Outline with selection:

ExpandFor first Outline in CurrentPage

That has property(selection) is not equal to "none"

Once you have an outline you can start inserting a Table. You need to insert Rows to the Table, then Cells to the Rows and finally Paragraphs to the Cells. You can then modify the text in the paragraphs:

ExpandFor first Outline in CurrentPage

That has property(selection) is not equal to "none"

ExpandInsert Table at the end

Modify property(bordersVisible) --> Set it to "true"

ExpandInsert Row at the end

ExpandInsert Cell at the end

ExpandInsert Paragraph at the end

Modify property(text) --> Set it to "A"

You can keep inserting Cells to the first Row, then add more Rows and more Cells. Note that each Row has to have the same number of Cells:

ExpandFor first Outline in CurrentPage

That has property(selection) is not equal to "none"

ExpandInsert Table at the end

Modify property(bordersVisible) --> Set it to "true"

ExpandInsert Row at the end

ExpandInsert Cell at the end

ExpandInsert Paragraph at the end

Modify property(text) --> Set it to "A"

ExpandInsert Cell at the end

ExpandInsert Paragraph at the end

Modify property(text) --> Set it to "B"

ExpandInsert Row at the end

ExpandInsert Cell at the end

ExpandInsert Paragraph at the end

Modify property(text) --> Set it to "C"

ExpandInsert Cell at the end

ExpandInsert Paragraph at the end

Modify property(text) --> Set it to "D"

Here is the XML for a full sample that inserts the following table. You can import it in Macro Editor and try it out. Note that it uses existing Outline on the page, so there needs to be at least one Outline on the page with selection for this to do anything:

A B C
1 2 3

<?xml version="1.0" encoding="utf-16"?>
<Macro name="Insert Sample Table" category="Table" description="Inserts a sample table" version="9">
  <For first="Outline">
    <That hasProp="selection" op="not" value="none" />
    <Insert type="Table">
      <ModifyProp name="bordersVisible" op="set" value="true" />
      <Insert type="Row">
        <Insert type="Cell">
          <Insert type="Paragraph">
            <ModifyProp name="text" op="set" value="A" />
          </Insert>
        </Insert>
        <Insert type="Cell">
          <Insert type="Paragraph">
            <ModifyProp name="text" op="set" value="B" />
          </Insert>
        </Insert>
        <Insert type="Cell">
          <Insert type="Paragraph">
            <ModifyProp name="text" op="set" value="C" />
          </Insert>
        </Insert>
      </Insert>
      <Insert type="Row">
        <Insert type="Cell">
          <Insert type="Paragraph">
            <ModifyProp name="text" op="set" value="1" />
          </Insert>
        </Insert>
        <Insert type="Cell">
          <Insert type="Paragraph">
            <ModifyProp name="text" op="set" value="2" />
          </Insert>
        </Insert>
        <Insert type="Cell">
          <Insert type="Paragraph">
            <ModifyProp name="text" op="set" value="3" />
          </Insert>
        </Insert>
      </Insert>
    </Insert>
  </For>
</Macro>

Comments

Name
Comment
BillD - 2016-03-07
I would like to be able to create cell dropdowns in a table that contain predefined lists of items that will populate the cell with the selected item, preventing incorrect values from being entered.  For example, if I have a column called STATUS that should have the following values in the dropdown: draft, out for review, approved, completed.  The functionality should work similar to Excel.

Separately, it would be nice to have an Add New Row button right above the table so the user doesn't have to use the right-click menu or the Ribbon.

Are either of these doable?

Thanks

Bill
SayHi - 2015-11-07
GOOD! I just wonder how to insert at the location of my current cursor?Thanks!
Cory - 2015-10-23
Is it possible to copy all tables from a section and put them in a new page with the macro creator? I have played around with it and it does not seem to currently be possible.

Other Posts

Show all posts