Is there a way to format data points when creating a chart using Apps Script? - google-apps-script

I want to write a Apps Script in Google Sheets that pulls data from a sheet and uses the conditional formatting of cells to colour the corresponding columns in a columns chart. This is easy to do in Sheets itself: once you've created the column chart you can just double-click on a individual column and then modify the format (or go to the Chart Editor, Customize, Series, Format then "Format data point). However, it seems to be missing from the chart configuration options: https://developers.google.com/apps-script/chart-configuration-options?hl=en#column-config-options
Am I correct that this is a missing option? If so, is there some way I could work around this?

Answer:
In spreadsheet embedded charts, it is not possible to specify the color for a specific column programmatically.
Reference:
Here is the list of methods for building and customizing a column chart: EmbeddedColumnChartBuilder. It doesn't include any method for this. setColors(cssValues) simply modifies the color of all columns in the chart, using the first value in the provided array.
This doesn't seem to be accessible in Sheets API either (see ChartSpec).
File a feature request:
If you think this functionality could be useful, I'd suggest you to request it in Issue Tracker using this template.

Related

Transferring conditional formatting to docs

I have google sheet with student data, with the help of a script (third party), I am automatically creating student reports with information from sheets. The reports are produced as PDF, however a google doc template is set for the report.
All the data is merge fine. However, is there a way to include in the script conditional formatting. I have cell's which are set to different colours based on the value of the cell. However, during the merge to the report, the formatting is not being replicated.
Can someone advise of a code to use?
1) What I am trying to achieve is if in the 'AboveOnBelowTarget column, if it is "-" the cell changed to red, IF "=" the cell changes amber and IF "+" changes to bright green. When making the reports the script creates PDFs using a google doc template, I'd like the colors to transfer to the Doc template.
Please find the script here

IMPORTxml on google sheets

Trying to make a spreadsheet on google sheets that scrapes data from a site.
I'm trying to get the sell price of items from rsbuddy exchange, eg:
https://rsbuddy.com/exchange/?id=1745
I have the code on google sheets as:
=IMPORTxml("https://rsbuddy.com/exchange/?id=1745","//*[#id='sell-price']")
But instead of showing me 1734gp it comes up as --- on the sheet.
I've tried adding /text() at the end of the query for the importxml but it doesn't change anything.
I'm guessing the solution is something similar?
I don't believe you can do it with xpath because it's populated dynamically. If you view the full source, you're getting what is there. It's literally ---
You can see the source data here which is in JSON and looks like it's tied to the page url id. Google sheets doesn't natively support json but this good person wrote a script and it seems to work well in my example sheet.
You'll need to do it via JSON, though you can't importJSON natively through Google Sheets. If you add this library as code to your sheet (via the script editor) then you can you can use =IMPORTJSON (with different parameters) to get the data back that you need.
http://blog.fastfedora.com/projects/import-json

Format Range of Numbers in Google Apps Script

I am a professor using Google Sheets and a pasted code in Script Editor to create a gradebook in which individual students can view only their grades. (For development of that code see this article.) I am having some trouble formatting numbers, however, as I need different formats for specific ranges from the spreadsheet. The current code formats numbers using:
if (typeof(value)=='number'){return Utilities.formatString('%.2f',value)}
This is within a longerfunction string(value)that also formats "string" and "objects."
I'm wondering if there's a way to customize that command so I can have percentages for a specific range and decimals for another? Or would I need to use a different function? (This article offers some options, but amateur that I am, I haven't figured out how to get any of the suggestions to work within the current code. I may simply need advice on where to paste an existing piece of code.)
First of all, it looks like the code that you are using is deprecated. The UiApp class (line 37. of your code) is deprecated. If I have more time, I could help you develop a new system, but for now it should work.
I would suggest formatting the output of the range en masse. I am assuming that for each gradebook entry, there is a specific range of information. Instead of formatting each cell based on the return values of string(value), I would use the setNumberFormats(object) method that is a part of SpreadSheet API.

Copying a working chart from a spreadsheet to a document results with "All series on a given axis must be of the same data type"

I have a Google spreadsheet that contains charts which are properly displayed. When I am using a GAS to copy these charts to a Google document some of the charts are properly displayed in the document while others (which are displayed properly in the spreadsheet) are displaying the message "All series on a given axis must be of the same data type".
Since these are working charts I am not sure why I get this message. Is there a workaround to this problem or a known issue?
Any advise will help.
Thx!
Based on KRR answer. Even though the chart might be working in Google spreadsheet it appears that the getBlob of the chart rechecks the source values of the chart. You need to make sure that the source values (in my case above from a spreadsheet) all have the same data type.

sheet.getCharts() not returning EmbeddedCharts

When using the getCharts() method of a Google Sheet instance, no charts are returned. This seems to be caused by the chart being a combo chart (rather than a more basic column, bar or pie chart). I modified the chart to be a column chart and getCharts() worked...returning a chart.
I can't find any confirmation that combo charts are completely ignored by the Spreadsheet App Script API.
Does anyone know of any SO questions, forum posts or official documentation that clarifies support (or lack thereof) for combo charts?
While there is no ComboChartBuilder for the Sheet class, I was hoping for something a little more explicit than the absence of a ComboChartBuilder. I would have thought getCharts() should return a Chart even if it's not modifiable.
There isn't a way to get a chart that isnt possible to build with apps script. You already found out the 'builder' class is missing.
There isnt a list of the unsupported ones, just the list of charts you can build with gas which are the sames you can get.