I want to make changes to existing google charts in my spreadsheet when new data is entered. I have and onEdit function that is working nicely but I cannot figure out how to get a "pointer" to the existing chart objects in the spreadsheet.
See Programmatically Managing Charts in Spreadsheets:
Also, Other Google I/O announcements may be of interest
I hate to have to break the bad news. But it is just not possible. There's no API at all to deal with charts in spreadsheets.
If it's not a problem in your use-case. You could generate the chart image/url automatically from the script using the Google Charts Tools and then insert the it on the spreadsheet.
Related
I have a Google Sheets spreadsheet with several tabs. Data is written to the 'tracker' tab constantly by a script which is writing information from incoming files. So new rows are constantly being added.
The tracker tab has a number of 'filter views' built using the UI. The data range that these filter views point to does not update when new rows are added.
I have seen some scripts which seem to be able to update the range if usng the Google Sheets API. However, I need something that runs within Google Apps Script itself..
Can anyone help?
The answer was to add the Google Sheets API via the add Services menu.
Simply, I have created a chart by inserting a chart and adding range/settings etc.
But I have realised that when resizing the chart (by recording a macro) all the settings are lost.
Is there a way to convert the Google Sheets Chart into Google Apps Script, otherwise I think I will have to re-create the graph in Google Apps Script. Just hoping to save some time with this query.
Thank you in advance.
I understood that you want to create a graph in Apps Script in a similar way that you did in a Sheet. If I understood it right, then you can complete your aim by using Charts Service. You can see a full example in the linked documentation.
Just to clarify it, first you would need to create a table using DataTableBuilder. This table should be equal to the one in your Sheet. After that you can create a graph based on that table. Please, ask me any question if you still have doubts.
After reading up a lot on the Google Spreadsheet API I have come to the conclusion that formatting (such as merging cells, changing fonts etc) is only available throught the Apps scripts.
Since we need to create and fill the spreadsheets with data programatically using Java on the back-end I guess I need to somehow either;
link the new sheet to a Apps script that trigger on-load or
create a Apps script that creates the spreadsheet for me.
Anyone knows?
If you want to just "create" the spreadsheet, you don't need a script to load whenever it spreadsheet is opened. It's probably easier to develop a script that runs once and create the spreadsheet for you.
Another tip is to have a template file that you can copy with most of the formatting (if not all) already there. Possibly pending just little things that are related to the real data the new spreadsheet will have.
Edit to answer the question in the title.
No, you can not add a script to an existing spreadsheet programatically, only manually. What you can do is previously set up a template spreadsheet with a script in it and create new spreadsheets by copying this template.
(answering the comment)
You can run a script programatically, but not upload it. To run a script you can deploy it as a web-app and call its url with either a http get or post (will call its doGet or doPost functions, that you must have declared). Also, you could set this script to run on form submit of any spreadsheet-form and just submit a set of answers to this form. At last (that I can think of now) you could just add the script as a library in another Apps Script and call it directly.
(Aug 2016) There is no way programmatic way to link a Google Sheet and Apps Script code other than manually. Based on what it seems you want ("create and fill the spreadsheets with data programatically using Java"), you can now do it without Apps Script.
TL;DR: Above, #Henrique has answered multiple questions and even questions that weren't asked! The good news is that today, we have more answers representing alternate possible solutions to what you're seeking.
It's now possible to "upload" Apps Script code programmatically with the
import/export system, say with Eclipse since you're a Java developer (2013 announcement).
I agree with Henrique's suggestion that if you create a spreadsheet
template, i.e., Excel file, you can use the Google Drive API to
programmatically import/create identical Google Sheets with all your
desired formatting.
"Formatting (such as merging cells, changing
fonts etc)" can now be done outside of Apps Script, as there is a
"new" Google Sheets API v4 (not GData).
In order to use the new API, you need to get the Google APIs Client Library for Java and use the latest Sheets API, which is much more powerful and flexible than any previous API. Here's one code sample to help get you started. If you're not "allergic" to Python, I also made a video with a different, slightly longer example introducing the new API and gave a deeper dive into its code via a blogpost that you can learn from.
Note the v4 API allows you to create spreadsheets & sheets, upload & download data, as well as, in the general sense, programmatically access a Sheet as if you were using the user interface (create frozen rows, perform cell formatting, resizing rows/columns, adding pivot tables, creating charts, etc.), but to perform file-level access such as uploads & downloads, imports & exports (same as uploads & downloads but conversion to/from Google Apps formats), you would use the Drive API instead.
Is there a way to publish a chart with a Google spreadsheet script? I dynamically create charts, but need a way to also dynamically publish these charts.
I am referring to the process of publishing a chart from the hidden menu at the top right of a chart. See below.
Unfortunately this functionality doesn't exist in Apps Script, but you can file a feature request on our issue tracker.
If the dynamic reports are replacing your charts like it did for me I just created a named range on the sheet that held the dynamic data. Then used =importRange to populate the sheet with the chart, then you publish the one time, my data pulls every hour, so my chart updates every hour. Hope it helps.
I have now written a number of different functions in Google Apps Script that I am using on a growing number of Google Spreadsheets.
Every time I make a small edit to one of these functions I then have to go into each spreadsheet and make the same correction.
Is there a way to "import" the functions to each spreadsheet so that I only have to edit the one "master" function and it is then updated on every spreadsheet?
Google has just release a new feature called Libraries, it is an elegant way to do what you're asking for. Here is the documentation, read it attentively because it's a multi-step process.
See this page for how to create and share functions in your sheets using Google Apps Scripts. The scripts are written in javascript and there are some limitations on how they can be used.
https://developers.google.com/apps-script/guide_writing_scripts#CustomFunctions