I am using Google Docs and Google Apps Script to make some auto generated report for our sprint.
I would like to add a Chart to my Sheet, and everything works fine with the following code:
var lChartBuilder = SpreadsheetApp.getActiveSheet().newChart();
lChartBuilder.addRange(SpreadsheetApp.getActive().getRange("Tasks!C39:S40"));
lChartBuilder.setOption("title", "Task Burndown");
var lChart = lChartBuilder.asLineChart().build();
SpreadsheetApp.getActiveSheet().insertChart(lChart);
But my series are organized horizontally, not vertically. In the editor I have seen the option "Switch rows / columns" and the other option "Use column C for labels"
I have tried many options (like lChartBuilder.setOption("reverseCategories", true); or lChartBuilder.setOption("isStacked", true);), but they seem all related to the last tab, I fear, not the Start tab.
So, is there a way (other than transposing my data) to do that or must I fire the chart editor manually to fix this each time I generate it?
Bonus question: how do I then set (through Google Apps Script as well) that the first row/column is a header and serves as legend?
For the bonus question, I have your bonus answer.
Use this option when creating/modifying your graph:
setOption('useFirstColumnAsDomain','true')
For an embedded chart, you will need to transpose your data, because the embedded chart uses spreadsheet data, and does not support transposition itself. However, you can handle this programmatically, and the transpose operation itself is simple.
In the code below, I'm assuming existence of a sheet named "Scratch" that will be used to store the working copy of the transposed data. You can create and then hide this sheet, so that it's not in the way.
// Uses 2D Arrays Library, see https://sites.google.com/site/scriptsexamples/custom-methods/2d-arrays-library
function buildChart() {
var taskSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Tasks");
var lChartBuilder = taskSheet.newChart(); taskSheet.removeChart(chart)
var srcData = taskSheet.getRange("Tasks!C39:S40").getValues();
// Transpose the table (using 2D Array Library)
var scratchData = ArrayLib.transpose(srcData);
var numRows = scratchData.length;
var numCols = scratchData[0].length; // assume all rows are same width
// Write scratch values to scratch sheet.
var scratchSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Scratch");
scratchSheet.getRange(1, 1, numRows, numCols).setValues( scratchData );
SpreadsheetApp.flush();
lChartBuilder.addRange(scratchSheet.getDataRange());
lChartBuilder.setOption("title", "Task Burndown");
var lChart = lChartBuilder.asLineChart().setPosition(1, 1, 1, 1).build();
taskSheet.insertChart(lChart);
};
For the bonus... Having the data formatted this way ensures the first row/column is a header and serves as legend.
Now, you've still got some problems to solve.
This code creates a new chart every time it runs, but you probably want to modify it instead. (Instead, you'll want to update the transposed data, and modify the chart to pick up the new range, if it changed.)
The X axis doesn't show all the task names - you may be able to control that.
You say you did not want to transpose your data. Unfortunately, there just isn't any way around that with the current incarnation of Charts - you've got to massage your data yourself. Inserting a widget with a different charting library could work, but widget support for Sheets has been deprecated. Fortunately, I had similar code already, so it was less work than it looks like. You should look at some of the other filtering capabilities in ArrayLib, you can do a lot with it.
Related
I have a GoogleSheet with basically two sheets, which are very similar in terms of data collected.
I need to calculate same values for both sheets, but source data is in different columns.
Therefore I created three files in AppsScript:
Common.gs - with common function definitions
sheet1.gs
sheet2.gs - both sheet1 and sheet2 have only definitions of proper ranges in particular columns and one function to run script, which essentially calls functions defined in Common.gs, like so in sheet1.gs:
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("sheet1")
var createdColumn = sheet.getRange("E2:E").getValues()
var ackColumn = sheet.getRange("G2:G").getValues()
var resColumn = sheet.getRange("I2:I").getValues()
var timeToAckColumn = sheet.getRange(2,14,ackColumn.length,1)
var timeToResColumn = sheet.getRange(2,15,resColumn.length,1)
var yearAndWeekRange = sheet.getRange(2,16,createdColumn.length,2)
function calculateMetricsSheet1() {
calculateTimeDiff(createdColumn, ackColumn, timeToAckColumn)
calculateTimeDiff(ackColumn, resColumn, timeToResColumn)
calculateWeek(createdColumn, yearAndWeekRange)
}
example function implementation (they are basically very similar with minor differences):
function calculateWeek(createdColumn, yearAndWeekRange) {
var arrData = []
for(var i=0;i<createdColumn.length;i++) {
if(createdColumn[i][0].toString()=="") {
arrData.push(["",""])
continue
}
var createdDate = new Date(createdColumn[i][0])
var year = createdDate.getFullYear()
var week = Utilities.formatDate(createdDate, "GMT+1", "w")
arrData.push([year, week])
}
yearAndWeekRange.setValues(arrData)
}
the sheet2.gs is basically different column definitions, the functions called within calculateMetricsSheet2() are the same.
So what is the problem?
The script works perfectly fine for sheet2.gs, but for sheet1.gs it does collect proper data, calculates proper data, but the data does not appear in proper columns after Range.setValues() call.
No exceptions or errors appear in the console.
Documentation does not provide any kind of information what could be the problem.
I have really ran out of ideas what could be the cause of the issue.
Does anyone have any idea what is going on?
edit: It may be useful to put emphasis on the fact that each script runs function calling 3 other functions -> all of them end with Range.setValues({values}). And for one sheet all of them work, and for the other - none.
That's the reason I assume there is something wrong with the sheet itself, maybe some permissions/protection? But I couldn't find anything :(
edit2: I modified my code to iterate through the sheet 10 rows at a time, because I thought maybe when I get a whole column, something bad happens with data and breaks setValues() function.
Unfortunately - even if my code iterated 1 row at a time, it still did not work on sheet1, but worked on sheet2. So not a data problem.
The code you show always puts values in yearAndWeekRange which is always in the 'sheet1' sheet. To put the data into another sheet, you need to change the target range appropriately.
The dimensions of the range must match the dimensions of the array you put there. Use this pattern:
yearAndWeekRange.offset(0, 0, arrData.length, arrData[0].length).setValues(arrData);
I found out what is the problem.
Two scripts were pretty identical, even with naming of variables - ie ackColumn, resColumn etc.
Those were stored as a global variables, so even if I was running script1.gs, it used global variables from script2.gs, effectively writing proper data to wrong sheet.
separating global variables names fixed the issue.
Perhaps a rookie mistake, but I missed the fact, that if I have a variable defined outside any function, it becomes global and could be overwritten from other file
So below is a dynamic graph this I made directly in google sheets. It updates automatically. So when I want to create a report, I use Apps Script to convert it to an image and copy it into a google doc.
Here’s my problem: The graph above represents a cummulative graph based on date ranges that the user picks.
What I now need is to creted daily graphs within that range to break these statistics down by day. Because I never know how many days will be in the range that they specify, I have to build these graphs programmatically then insert them into the google sheet.
I have figured out how to do this, but the closest I’ve been able to get in making the daily graphs look like the one above is shown below:
I simply cannot find the information I need to make the second chart look similar to the first one. Anyone have advice that might help?
You can copy your existing chart and modify its properties to generate a new chart.
Sample Code:
function insertChart(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet5");
var charts = sheet.getCharts();
for(var i in charts){
if(charts[i].getOptions().get('title')== 'Types of Incidents Over Time'){
var chart = charts[i];
var modifiedChart = chart
.modify()
.clearRanges()
.addRange(sheet.getRange("A25:B29"))
.setOption('title','New Chart')
.setOption('vAxis.gridlines', {minSpacing: 2})
.setOption('vAxis.minorGridlines.count', 1)
.setPosition(25,5,0,0)
.build();
sheet.insertChart(modifiedChart);
}
}
}
Output:
What it does?
Get all the existing chart in a specific sheet using Sheet.getCharts()
Select your desired chart based on its chart title using EmbeddedChart.getOptions() and ChartOptions.get(option). For a list of available options for Column Chart, please refer here.
Once your desired chart was selected, you can modify the properties of the selected chart using EmbeddedChart.modify(). It will return an EmbeddedChartBuilder where you can configure your chart's properties. See EmbeddedChartBuilder Methods for additional information
In the sample code, I cleared the existing range first before setting a new data range. After that, I configured some chart options such as the Title, Vertical Axis Gridlines Minspacing, Position, etc.
Note: I had to configured the vAxis.gridlines minspacing manually since when I tried to get the vAxis.gridlines of the existing chart it returned a null value. Chart Options details are discussed here.
Once you have configured your chart, you need to build chart to reflect all changes made to it using EmbeddedChartBuilder.build() which will create an EmbeddedChart
Insert your newly created EmbeddedChart using Sheet.insertChart(chart)
I have a Google Sheet I use to track data. I have a sheet that pulls data from multiple sheets in a single row. The row has the current date for Column B and then pulls in data for columns C through AC. I am trying to create a mechanism to snapshot that data and put it on the next line below it. I want the ability to continue doing this and keep pushing the data down and dropping the current on the next line. This allows me to select data in column A to use for graphing purposes. This is what I was using:
function recordHistory() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("History");
var source = sheet.getRange("B2:AC2");
var values = source.getValues();
var now = new Date();
values[0][0] = now;
for (var col in values) {
sheet.getRange(sheet.getLastRow(),2,1,28).setValues(values[col]);
}
I used a combination of examples and I think I got my wires crossed with the translation from one to the other. Looking for help to clean this up or point me to a better option. I was originally using appendRow, but that limits me to using the first column. I want the ability to have the snapshot placed in the 2nd column and the corresponding columns after it. Hopefully, that makes sense.
In this sheet, you can see I am pulling data from the first 2 sheets into the last sheet. I am skipping the first column and using Row 2 as the exact values. The script above is supposed to take what is in Row 2, snapshot it as values only, and move the data to Row 3, moving the previous rows down. This provides me a history of the values. I will be using the triggers to run this function every night at midnight, so the data will be a daily capture of the values. Hopefully, this makes it a bit more clear.
EDIT 2: Let me try and simplify the explanation. I have a sheet that has data in cells B2 through AC2. I want to grab that data and copy it to cells B3 through AC3, moving the data down a row. So on the sheet, you should see cells B3:AC3 having yesterdays data. B4:AC4 has the day before. B5:AC5 has the day before that. Basically keeping a log of the data that is captured in B2:AC2 each day.
Is it clearer what I am trying to accomplish or should I explain it further? I really want to get this script corrected so I can schedule it to run over the weekend.
After a few hours of playing with syntax a bit and realizing where my mistake was, I noticed some issues with the way I was capturing the data and trying to apply it to a range. Here is the solution to my problem:
function recordHistory() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("History");
var source = sheet.getRange("B2:AC2");
var values = source.getValues();
sheet.insertRowBefore(3);
sheet.getRange(3, 2, 1, 28).setValues([values[0]]);
};
As you can see in the solution, I realized how the data was being stored in the array and matched it to the setValues part of the script. It is a pretty basic issue I was having, but the use case was difficult to explain. The insertRowBefore was also a vital piece to establish the structure of the sheet.
Is there a way to auto save data entered in a temp area (risk is a calculated value based on the values entered) on google sheet. I have a working space and all my logs is now needing to be saved for later review.
see sample sheet.
Created a sample data screenshot
Thanks
There's two ways to do it. You'll need to create a log of sorts and have the dashboard reference the bottom most entry. If you have App Script experience, that would be the better solution, however without it you could use the a Google Form for editing the dashboard. There wouldn't be any formulas alone that will work for this due to needing to hardcode the inputs, and formulas can only return values as arrays (mirror/change values in other cells).
You can use a Google Form that is linked to the spreadsheet so that someone has to submit the form with the inputs to change the dashboard. You would then use a =Max() function on the timestamp column, and then either Vlookup or Index(match()) to return the variables for the dashboard based off Max(timestamp).
The alternative method would be to create basically set of cells similar to the input table, and add a button that if clicked, takes, the values and updates them in the variables for the dashboard, but also logs them on another sheet. (It would be something like this)
Thank you all for the suggestions. I end up using the below script to accomplish the task.
function FormExec() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sinput = ss.getSheetByName("sheet1");
var soutput = ss.getSheetByName("sheet2");
var input = sinput.getRange(14, 3, 15).getValues();
var flatin = [].concat.apply([], input);
soutput.getRange(soutput.getLastRow()+1, 1,1, 15).setValues([flatin]);
soutput.insertRowAfter(soutput.getLastRow());
Logger.log(input);
}
So I have this google script that I want to use to create charts in my spreadsheet. I'm basically programatically creating content (with the use of spreadsheet data) that I then want to plot. The way I used to do it is by filling one of the sheets with all the data and then using that data to plot, but I was hoping to skip that step and feed the javascript arrays directly into my addRange method.
So I've got a script that creates a new chart:
// insert the scenario chart
var scenarioChartBuilder = sheet.newChart();
scenarioChartBuilder.setPosition(5, 6, 5, 5)
.setChartType(Charts.ChartType.AREA)
.addRange(rangeObject);
sheet.insertChart(scenarioChartBuilder.build());
The problem is; how do I make "rangeObject", given that I only have javascript arrays, and don't want to use actual spreadsheet data? Or is there another way of plotting data that isn't actually in a spreadsheet?
Range data is actually just a multidimensional array.
So a rangeObject could just be defined like;
var rangeobject = [[data, data, data],[data, data, data]];
The first array represents the row and the second array the column data.
programmatically you could get the data like;
var dataFirstRowSecondColumn = rangedata[0][1]; //0 indexed array!
So, to add a range is just to pass a multidimensional array (with content data).
But beware ;-) When adding to a chart i would think that you would have to mind that each column would only contain on kind of data to be valid.
In code you could directly use my first example.