R melt for Google Apps Script and Excel/VBA: reshaping/unpivoting data

In Starter implementation of R-melt for Excel I introduced a one liner in Excel/VBA to transform this
into this

 

You could consider this to be a kind of un-pivot – exploding tabulated data back into transactional rows as described by Hadley Wickhams in his implementation of reshape/melt for R.

Following a post by Tony Hirst I figured we could do with an Excel version, and now a Google Apps Script version. You’ll notice that I used more less the same javaScript syntax for options in both the VBA and GAS versions. I’ll probably be doing more of this in the future, so here is a quick write up on that.

I’m using the mcpher GAS libraries and in fact implemented this as a new module of those libraries. This means that all that is needed in the calling module for the example above, aside from a reference to the mcpher libraries, is this.

function testMelt(){
mcpher.reshapeMelt ({outputSheet:"meltOut",inputSheet:"melt",id:["id","time"]});
}

The code for this module is in this gist, the test spreadsheet here, and the complete library code here.

If you prefer Excel, the VBA example looks like this and can be downloaded here.

Public Sub testMelt()
    reshapeMelt "{'outputSheet':'meltOut','id':['id','time']}"
End Sub

For more stuff like this, visit the ramblings site or the associated blog. If you have suggestion for particular topic ocontact me on our forum 

About brucemcp 225 Articles
I am a Google Developer Expert and decided to investigate Google Apps Script in my spare time. The more I investigated the more content I created so this site is extremely rich. Now, in 2019, a lot of things have disappeared or don’t work anymore due to Google having retired some stuff. I am however leaving things as is and where I came across some deprecated stuff, I have indicated it. I decided to write a book about it and to also create videos to teach developers who want to learn Google Apps Script. If you find the material contained in this site useful, you can support me by buying my books and or videos.