Note that this article uses the Apps Script UI service, which has since been deprecated. I leave it here for interest.
The equivalent of inserting a shape in Google Docs is to insert a drawing, into which you have inserted shapes. Unfortunately it is not at this time possible to script that. It is possible (I think likley) that this will be possible in the future, so for the interim, I want to abstract away the shapes so that the method can be modified in the future.
The only thing that can work for the moment is to use the UI creator, along with an absolutepanel, and some kind of object like a label. This is not that great because
In any case it will do for now. So far I have this for the From VBA to Google Apps Script conversion of the Roadmap Generation project. You can find the code (its a work in progress) and test data in the VBA to Google Apps Script Roadmapper project download
It sucks a bit compared to the Excel version, but I'll keep tweaking.
Since I want to abstract the shapes, I created my own cShape class, which is currently implemented using Google UI services, but since this is just a hack, I will want to be able to use Google Drawings if they ever become accessible via scripting. This abstraction will mean that I can switch over with minimum effect later.
Since this is actually using the UI, what in fact is needed behind the scenes is to create a one off UiApp, and a single AbsolutePanel, and a Label for each roadmap shape required. Since I dont want the management of all that to be the responsibility of the caller, this is all handled automatically, like this, with the whole structure being managed through the public variable, shapePanel.
To enable abstraction, and also because the UIPanel doesnt actually provide access to properties to such as current height etc, any properties of interest are managed through useful properties in the cShape class. In fact, the actual setting of width position and so on are only actually committed to the underlying shape once when complete. This means that the z-order becomes a function of when you commit the shape, not when you create it. The other benefit of this is that I can minimize the number of calls I make to the API.
Mimic the Excel addshape. Note that for the moment, the shapeType is ignored, since i can only do rectangles using the UIApp.
Here are a few examples of setting positions etc.
Note the use of the .commit() method, which does the below. In other words, all the shape attributes aregenerally all set before committing. An annoying characteristic of the UIApp and the AbsolutePanel, is that they do not self size. Part of the function of the commit method is to automatically tweak the AbsolutePanel so that it is always big enough to show all the shapes.
the show() method finally will display all the committed shapes, and tweak the UiApp so that it is big enough for the AbsolutePanel
You can find the code (its a work in progress) and test data in the VBA to Google Apps Script Roadmapper project download
For more like this, see From VBA to Google Apps Script . Why not join our forum,follow the blog or follow me on twitter to ensure you get updates when they are available. Much of the code referred to here is the mcpher library. You can find out how to include it in your project here.
The code is in the mcpher library. See here for how to incorporate it into your project.
Take a look at From VBA to Google Apps Script for more like this.. In the meantime why not join our forum,follow the blog or follow me on twitter to ensure you get updates when they are available.
Services > Desktop Liberation - the definitive resource for Google Apps Script and Microsoft Office automation > From VBA to Google Apps Script >