<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>hi, it&#39;s mike</title>
    <link>https://mike.puddingtime.org/tags/numbers/</link>
    <description>Recent content on hi, it&#39;s mike</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <managingEditor>mike@puddingtime.org (mike)</managingEditor>
    <webMaster>mike@puddingtime.org (mike)</webMaster>
    <copyright>© 2026, mike</copyright>
    <lastBuildDate>Sat, 21 Aug 2010 07:00:34 +0000</lastBuildDate>
    <atom:link href="https://mike.puddingtime.org/tags/numbers/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Let&#39;s Hear It for Lossy</title>
      <link>https://mike.puddingtime.org/posts/2010-08-21-let-s-hear-it-for-lossy/</link>
      <pubDate>Sat, 21 Aug 2010 07:00:34 +0000</pubDate><author>mike@puddingtime.org (mike)</author>
      <guid>https://mike.puddingtime.org/posts/2010-08-21-let-s-hear-it-for-lossy/</guid>
      
      <description>So, I’ve got this Excel spreadsheet that represents a dump of over 2,000 articles from an old CMS. Using the same general workflow I used before, I’m importing the spreadsheet into a local MySQL database using the Ruby spreadsheet gem. The importer I wrote just reads line by line, creates a new …</description>
      <content:encoded><![CDATA[<p>So, I&rsquo;ve got this Excel spreadsheet that represents a dump of over 2,000 articles from an old CMS. Using the same <a href="http://bit.ly/aMX94S">general workflow I used before</a>, I&rsquo;m importing the spreadsheet into a local MySQL database using the Ruby spreadsheet gem. The importer I wrote just reads line by line, creates a new ActiveRecord object then saves it.</p>
<p>Two thousand lines of spreadsheet &ndash; two thousand lines that include news-length content in each of their lines &ndash; is enough to make Excel (on the Mac) fall to its knees whining. Worse, after a few changes and saves, the spreadsheet library couldn’t even read the content. I tried saving it between .xls and .xlsx a few times and the problem persisted. I thought about pasting just the data into a blank spreadsheet to see if that would shake whatever rot had settled into the spreadsheet&rsquo;s soul, but getting Excel to copy and paste from a spreadsheet that big just made it beachball and crash.</p>
<p>So it was in a state of agitation that I decided to try one last thing: I opened the spreadsheet in Numbers, re-saved it as an Excel file and tried reading that in to my importer script. It not only worked, but the execution time of the script itself went from somewhere around 10 seconds to about 1.5. It was so much faster I rechecked my script, thinking I&rsquo;d commented out the line that actually saved the record.</p>
<p>I have no idea what goes on inside an Excel file, but if I had to guess, &ldquo;a lot of crap&rdquo; would probably be a big part of it. Some of that crap is, no doubt, useful to someone somewhere who&rsquo;d just die if they couldn&rsquo;t have this or that feature that&rsquo;s enabled by this or that bit of crap. I&rsquo;d also bet that there are people who are bitterly disappointed that Excel spreadsheets they&rsquo;ve opened or saved in Numbers have quietly stripped out some essential bit of crap they <em>need</em> to do their work. As near as I can tell, Numbers stripped out a bit of crap that kept me from doing mine. It&rsquo;s almost like discovering that high-compression JPEGS make bad pictures look better.</p>
<p>#excel #numbers</p>
<p><em><a href="mailto:mike@puddingtime.org?subject=Re%3A%20Let%27s%20Hear%20It%20for%20Lossy">Drop a line about this</a>.</em></p>]]></content:encoded>
    </item>
    <item>
      <title>Scripting iWork Numbers &#39;09 With Appscript</title>
      <link>https://mike.puddingtime.org/posts/2009-03-12-scripting-iwork-numbers-09-with-appscript/</link>
      <pubDate>Thu, 12 Mar 2009 00:53:09 +0000</pubDate><author>mike@puddingtime.org (mike)</author>
      <guid>https://mike.puddingtime.org/posts/2009-03-12-scripting-iwork-numbers-09-with-appscript/</guid>
      
      <description>Simple work problem: I need to keep a log of stories published on the sites I cover. Part one of the problem is getting all the existing stories into the log. Part two of the problem is getting new stories into the log as I publish them from now on.
I was pretty excited about taking Numbers ‘09’s …</description>
      <content:encoded><![CDATA[<p>Simple work problem:  I need to keep a log of stories published on the sites I cover. Part one of the problem is getting all the existing stories into the log.  Part two of the problem is getting new stories into the log as I publish them from now on.</p>
<p>I was pretty excited about taking Numbers &lsquo;09&rsquo;s scripting support out for a spin. Unfortunately it&rsquo;s a little anemic, especially compared to full-featured Ruby libraries like <a href="http://roo.rubyforge.org/">roo</a> and <a href="http://spreadsheet.rubyforge.org/">spreadsheet</a>. For instance, it doesn&rsquo;t really have a conception of &ldquo;the last row with data in it,&rdquo; as the other two do.  So you can&rsquo;t just plop new data into the first empty row without doing something like iterating through all rows and finding the first one where a particular cell is empty. I uploaded <a href="http://mph.puddingbowl.org/ruby/Numbers-rb/index.html">the appscript-exported Numbers scripting dictionary</a> if you&rsquo;re curious about what it can do.</p>
<p>There&rsquo;s some interesting room to grow for both scripts.  In addition to tracking stories, we have to keep up with their overall performance.  With the <a href="http://github.com/robmckinnon/rugalytics/tree/master">rugalytics</a> gem it&rsquo;s possible to mine performance data from Google Analytics based on the title of a given article or regular expressions taken from the URL.</p>
<p>Anyhow, this one solves part one.  It&rsquo;s a one-time-only importer that runs through the indexes for each container and grabs everything:</p>
<pre><code>    [sourcecode language=&quot;ruby&quot;]

    !/usr/bin/env ruby

    require 'rubygems'

    require 'appscript'

    include Appscript

    require '/Users/mph/lib/ruby/CDEV.rb'

    require 'hpricot'

    require 'open-uri'

    include Hpricot

    num = app(&quot;Numbers&quot;)

    ss = num.open(&quot;/Users/mph/Desktop/Invoices.numbers&quot;)

    sheet = ss.sheets[&quot;Invoices&quot;]

    table = sheet.tables[&quot;Current Quarter&quot;]

    containers = [&quot;netsysm&quot;, &quot;nethub&quot;, &quot;netsecur&quot;, &quot;linux_unix&quot;, &quot;netos&quot;, &quot;windows&quot;]

    base_url = &quot;http://enterprisenetworkingplanet.com/&quot;

    cdevids = []

    containers.each do |con|

      page = Hpricot(open(&quot;{base_url}{con}&quot;))

      index = page.search(&quot;//div[@class='section_index_list'&quot;)

      articles = index.search(&quot;//div[@class='post']&quot;)

      articles.each do |a|

        link =  a.search(&quot;//h3/a&quot;).first

        url =  link[:href]

        art = CDEV.new(url)

        unless cdevids.include?(art.id)

          cdevids &lt;&lt; art.id

          title = art.title

          url = art.url

          author = art.author

          pubdate = art.pubdate.strftime(&quot;%m/%d/%y&quot;)

          row = table.rows.add_row_above(table.rows[2])

          table.rows[2].cells[1].value.set(author)

          table.rows[2].cells[2].value.set(pubdate)

          table.rows[2].cells[3].value.set(title)

          table.rows[2].cells[6].value.set(url)

        end

      end

    end

    [/sourcecode]
</code></pre>
<p>This one is for part two.  It&rsquo;s just part one stripped down and cast toward working on the foremost window in Safari, so when I&rsquo;m giving an article a look from the front-end to make sure the CMS didn&rsquo;t mangle it, I can run the script and capture the article info in my spreadsheet.</p>
<p>This one points to another oversight in the existing dictionary for Numbers &lsquo;09:  One of the new features in the software is something called &ldquo;Table Categories.&rdquo;  You can pick any column and create a category based on its content.  It&rsquo;s handy for things like quarterly invoicing, because you can take a lump of rows, create a table category out of the publication date and get a nicely sorted table with quarterly subtotals for each month. That poses two problems for my script:</p>
<ol>
<li>
<p>When a spreadsheet has active Table Categories, you can&rsquo;t add a row to it.</p>
</li>
<li>
<p>The Table Categories functionality doesn&rsquo;t exist in the current Numbers scripting dictionary.</p>
</li>
</ol>
<p>So if the script is run while the table is categorized, it fails. It doesn&rsquo;t have any way to query the table and figure out if it&rsquo;s categorized, so I had to fall back on UI scripting to look for whether the &ldquo;Table&rdquo; menu has an item that reads &ldquo;Disable All Categories.&rdquo;  If it does, the script clicks the menu item. Once the script is done adding stuff, it clicks a menu item called &ldquo;Enable All Categories&rdquo; to put the table into a more readable state again.</p>
<p>I hate UI scripting. <a href="http://blog.zenspider.com/2007/07/applescript-is-a-pita-exportin.html">Another blog entry</a> gave me a head start on figuring out what to call the menu items I needed to find, and <a href="http://appscript.sourceforge.net/tools.html">ASTranslate</a> did me the favor of cutting through all the Applescript hyperventilation and rendering things down to something more usable.</p>
<p>So anyhow:</p>
<pre><code>[sourcecode language=&quot;ruby&quot;]



!/usr/bin/env ruby

require 'rubygems'

require 'appscript'

require '/Users/mph/lib/ruby/CDEV.rb'

include Appscript

num = app(&quot;Numbers&quot;)

saf = app(&quot;Safari&quot;)

num_sys = app(&quot;System Events&quot;).application_processes[&quot;Numbers&quot;]

num_cat_menu = num_sys.menu_bars[1].menu_bar_items[&quot;Table&quot;].menus[1]

num_cat_disable = num_cat_menu.menu_items[&quot;Disable All Categories&quot;]

num_cat_enable = num_cat_menu.menu_items[&quot;Enable All Categories&quot;]

ss = num.open(&quot;/Users/mph/Desktop/Invoices.numbers&quot;)

sheet = ss.sheets[&quot;Invoices&quot;]

table = sheet.tables[&quot;Current Quarter&quot;]

num.activate

table.activate

page = saf.documents[0].get

url = page.URL.get

art = CDEV.new(url)

title = art.title

url = art.url

author = art.author

pubdate = art.pubdate.strftime(&quot;%m/%d/%y&quot;)

num_sys.frontmost.set(true)

if num_cat_disable.exists

 num_cat_disable.click

end

row = table.rows.add_row_above(table.rows[2])

table.rows[2].cells[1].value.set(author)

table.rows[2].cells[2].value.set(pubdate)

table.rows[2].cells[3].value.set(title)

table.rows[2].cells[6].value.set(url)

num_cat_enable.click

 [/sourcecode]
</code></pre>
<p>#appscript #iwork #numbers #ruby</p>
<p><em><a href="mailto:mike@puddingtime.org?subject=Re%3A%20Scripting%20iWork%20Numbers%20%2709%20With%20Appscript">Drop a line about this</a>.</em></p>]]></content:encoded>
    </item>
  </channel>
</rss>
