# Is this code supposed to generate a table in layout?

**URL:** <https://forums.sketchup.com/t/is-this-code-supposed-to-generate-a-table-in-layout/223023>\
**Category:** Ruby API\
**Tags:** layout\
**Created:** [March 11, 2023, 7:42am UTC](https://forums.sketchup.com/t/is-this-code-supposed-to-generate-a-table-in-layout/223023 "2023-03-11T07:42:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rtches](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/rtches/32/131827_2.png) [@rtches](https://forums.sketchup.com/u/rtches)\
**Post date:** [March 11, 2023, 7:42am UTC](https://forums.sketchup.com/t/is-this-code-supposed-to-generate-a-table-in-layout/223023/1 "2023-03-11T07:42:09Z")

</div>

Is this code supposed to generate a table in layout?  
If I open the file I don’t see a table…

```rb
def openLayoutDoc()
  @layoutfilename = UI.openpanel("Open Layout File", "c:/", "LAYOUT Files|*.layout||")
  myarray = @layoutfilename.split('.')
  UI.messagebox("Not a layout file, please open a layout file and try again") unless myarray.at(-1).to_s.downcase == "layout"
end

openLayoutDoc()
bounds = Geom::Bounds2d.new(1, 1, 4, 4)
rows = 4
columns = 4
table = Layout::Table.new(bounds, rows, columns)
doc = Layout::Document.open(@layoutfilename.to_s)
doc.add_entity(table, doc.layers.first, doc.pages.first)
anchor_type = Layout::FormattedText::ANCHOR_TYPE_TOP_LEFT
start_point = Geom::Point2d.new(1, 1)
text = Layout::FormattedText.new("Hello LayOut", start_point, anchor_type)
table[1, 1].data = text

```

---

<div class="post-metadata">

**Author:** ![dezmo](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/dezmo/32/51058_2.png) [@dezmo](https://forums.sketchup.com/u/dezmo)\
**Post date:** [March 11, 2023, 8:40am UTC](https://forums.sketchup.com/t/is-this-code-supposed-to-generate-a-table-in-layout/223023/2 "2023-03-11T08:40:42Z")

</div>

> [@rtches](#):
>
> If I open the file I don’t see a table…

Most likely because there is no save method applied after the changes on that particular Layout document. (Otherwise I did not checked the ccde)

The [#save](https://ruby.sketchup.com/Layout/Document.html#save-instance_method) method saves the [Layout::Document](https://ruby.sketchup.com/Layout/Document.html) to a file at the given path.

---

<div class="post-metadata">

**Author:** ![rtches](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/rtches/32/131827_2.png) [@rtches](https://forums.sketchup.com/u/rtches)\
**Post date:** [March 11, 2023, 12:04pm UTC](https://forums.sketchup.com/t/is-this-code-supposed-to-generate-a-table-in-layout/223023/3 "2023-03-11T12:04:19Z")

</div>

Thanks @dezmo.  
It was partially the problem. The problem was also that the file was opened.  
So I have to test if file is opened and force user to close it. After that, place the table and save the document.

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [March 11, 2023, 8:12pm UTC](https://forums.sketchup.com/t/is-this-code-supposed-to-generate-a-table-in-layout/223023/4 "2023-03-11T20:12:49Z")

</div>

You don’t need these …

```ruby
myarray = @layoutfilename.split('.')
... unless myarray.at(-1).to_s.downcase == "layout"

```

Just use something like …

```ruby
  msg = 'Not a layout file, please open a layout file and try again.'
  UI.messagebox(msg) unless File.extname(@layoutfilename) == ".layout"

```

* * *

(Reminder use shorter code lines for clarity.)
