Google Apps Script SpreadsheetApp Banding Class - How to access these methods - google-apps-script

I am trying to figure out how to access the Banding Class of methods for the SpreadsheetApp. This is not how to simply apply banding. Instead, I am trying to get the data related to a sheet that banding has been applied to.
Here is the Developers Documentation:
https://developers.google.com/apps-script/reference/spreadsheet/banding
I can use .getBandings() but this simply confirms whether or not a banding exits in the sheet. There is no data as to the details of that banding. For example, I would like to return the range of the banding.
I also learned that when a banding is applied, the background color of the cell stays default, so I cannot utilize those methods to determine the details.
Here is the link to a Sheet with banding applied. I have added a few lines of script to show what getBandings() returns. Feel free to make a copy.
https://docs.google.com/spreadsheets/d/1xRuwE8moueSY5ZizZcy6KPkVmhJmOITN9Bql7XgHY3g/edit#gid=0
Any advice on how to access/utilize the methods in the Banding class would be appreciated.
From a comment to an answer
I am trying figure out the list of methods that would open up that class in SpreadsheetApp. I have experimented with SpreadsheetApp, getActiveSheet, getSheetByName, getRange and more. I cannot figure out how to open up that set of commands in the Banding class.

For example, I would like to return the range of the banding.
To get the range of a banding use the getRange() method of Class Banding
getBandings() returns an interable object. Below is a simple example of how to get the address of the range of a banding. It assumes that the spreadsheet has at least one banding.
function myFunction(){
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var bandings = spreasheet.getBandings();
var range = bandings[0].getRange();
Logger.log(range.getA1Notation());
}
Related
Apply a "row banding" theme to a range

Related

Creating a Scatter Graph using Scripting for Google Sheets?

Google sheet and script: https://docs.google.com/spreadsheets/d/1Vq2MYQzbaRoYg31IRkwNOofJYYOH82ElSdN7miExiVQ/edit?usp=sharing
I currently am trying to create a scatter graph using data that I have been given but only using script, so I am making this scatter graph based on the data in B15 tp W16 but this data is placed in these cells by using the new applied menubar option called "Apply Script".
So if you click:
Apply Script > Activate Script
This will change the cell sizes, change the font size, create a conditional format for the data within cells C3 to W13, and will create a key for the data as well.
Then if you click:
Apply Script > Select Power Input > Select any option
Once you select which row of data you want to view, it'll be applied with cells B15 to W16 (It will make more sense if you try it yourself).
These functions will be applied to any given data that is active, I have 3 sheets within the document below so it demonstrates this.
So now that I have explained how my scripting works so far, I want to know how to script a scatter graph using any given data that is cell B15 to W16. Everything I have demonstrated above is all that I have taught myself in the past 3 days watching videos but I can't find any demonstrations for creating a chart on google scripting anywhere, I have also searched through script.google.com to find the coding needed but I couldn't understand where to start and how to present it. So if anyone can point me in the right direction that'll be great, thank you!
https://docs.google.com/spreadsheets/d/1Vq2MYQzbaRoYg31IRkwNOofJYYOH82ElSdN7miExiVQ/edit?usp=sharing
This is my existing chart script:
function lineGraph() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var row = sheet.getRange( 15, 2, 2, 22).getValues();
sheet.newChart().addRange().asScatterChart().getChartType().SCATTER;
}
Your script is certainly in the right direction, but you have to specify where to place your chart in the sheet by using setPosition.
Also, I added some options for the generated chart to make more sense based on your input.
Sample Code:
function lineGraph() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var row = sheet.getRange("B15:W16");
var chart = sheet.newChart()
.asScatterChart()
.addRange(row)
.setPosition(18,2,0,0)
.setTransposeRowsAndColumns(true)
.build();
sheet.insertChart(chart);
}
Sample Output:
Note that this is bare bones, you can specify more options by adding more methods onto the chartBuilder. Please see reference here:
Embedded Scatter Chart Builder

What's the difference between ui.alert and Browser.msgBox?

When working with Google Scripting, there's a Browser.msgBox(); (Link) and ui.alert(); (Link). What is the difference between the two? They appear to do the exact same thing.
There are more methods within, such as Browser.inputBox(); and ui.prompt(); which again, appear to be identical.
The Browser Class is only available to a Spreadsheet. The Ui Class can be more widely used. Unfortunately, the documentation for Class Ui only shows an example of the getUi() method with the SpreadsheetApp Class. But getUi() is available to DocumentApp.
DocumentApp.getUi()
And to:
FormApp.getUi()
If you try to call Browser.msgBox() from the wrong context, you'll get an error:
Cannot call Browser.msgBox() from this context; have you tried Logger.log() instead?
Browser.msgBox() is easier to use in a Spreadsheet script. You don't need to first use var ui = SpreadsheetApp.getUi();
To compare:
Browser.msgBox('prompt here');
SpreadsheetApp.getUi().prompt('prompt here');

correctly insert chart from Google Sheet into Document using Google Apps Script

I want to copy charts from my spreadsheet to a document using Google apps script.
Inserting the charts works, but there is an issue with a) permissions and b) formatting.
Charts can be inserted as follows:
var b = d.getBody();
var charts = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("charts").getCharts();
for (var i in charts) {
b.appendImage(charts[i]);
Logger.log(charts[i].getBlob().getName()); // correct: "chart.png"
}
When I manually copy the chart and paste it in the document, the formatting is exactly as in the spreadsheet.
when I copy using the script, formatting looks very strange or even causes an error (for the line chart with dates on the x-axis it shows an image with the text Data column(s) for axis #1 cannot be of type string)
Top row is manually copied, bottom row is the result of the script.
The second -related?- issue: the spreadsheet needs to be shared ("anyone with the link can view"), otherwise it shows the image (correct name, width, height, etc.) as an error message: User not signed-in. Sign in.
Any suggestions how to insert the chart with the correct formatting and without having to share the spreadsheet?
Stefan pointed me in the right direction (thanks!).
It appears the rendering is done quite differently when pasting/saving images.
Different axis settings, colors, theme, etc.
I ended up manually creating the chart. E.g. for the first bar chart, the following results in a chart that appears the same in the spreadsheet and in the document:
var chart = dest.newChart();
chart
.setChartType(Charts.ChartType.BAR)
.addRange(myrange)
.setPosition(2,8,0,0)
.setOption("theme","maximized")
.setOption("colors",["#3366CC","#FF9900","#DC3912","#109618"])
.setOption('isStacked', true)
.setOption('width', 500)
.setOption('height', 130)
.setOption('hAxis.viewWindow.max', countActions)
.setOption('vAxis.gridlines.count', 0)
.setOption('legend', {position: 'in', textStyle: {fontSize: 12}})
var chart = chart.build();
dest.insertChart(chart);
I also had to change the data range, as it appears the following chart settings can not be set manually.
switch rows/columns
use first row as header
To solve that I have manually transposed the range in the spreadsheet (=TRANSPOSE(original_range)) and let myrange point to that new range, and I have added an empty column so that one is used as an empty header.
I have not looked at the line chart yet, it could be the Chart API can not create a timeline in the same way as the Spreadsheet does.
For what its worth I just used the original script in a simple report builder that takes data and charts from a Google Sheet and auto-generates a report containing them for any user. It worked very easily. Was a slightly modified version of the following
var charts =
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("charts").getCharts();
for (var i in charts) {
doc.getBody.appendImage(charts[i]);
Logger.log(charts[i].getBlob().getName())};
where in my script "doc" is a var that repr
Have a look at:
Reading title of Graph
When I was playing around, I remember having issues as well like you described. When I made sure all the required fields were added in the graph (so no skipping of data elements), it worked fine.
Strangly, skipping data elements didn't seem to cause issues in the graph (neither with manually copying & pasting).

Displaying Google Spreadsheets data in Google Sites as HTML?

I have a google spreadsheet and I'm trying to build a webpage using Google sites. I don't want to embed the whole sheet, or even a range of cells. I just want my webpage to display text from some cells in the spreadsheet, and I want to be able to style that text using HTML.
For example, in the value in cell A1 is "There are 3 games today".
I want to be able to put this text in my webpage and then give it a font size, alignment, and put it in a box with a border and curved corners (border radius).
I think there may be several ways of acheiving this but the closest I have got is by inserting an 'Apps script gadget' into the page and using the doGet() method:
function doGet(e){
var app = UiApp.createApplication();
var ss = SpreadsheetApp.openById('SPREADSHEET_KEY');
var sheet = ss.getSheetByName("Sheet1");
var range = sheet.getRange('A1');
var dataRange = range.getValue().toString();
app.add(app.createHTML("There are " + dataRange + " games today"))
return app;
}
but I dont know how to then add HTML to the webpage to make it look fancy.
I haven't even been able to set anything within the script e.g. font alignment. I've tried
.setHorizontalAlignment('center')
but I keep getting errors such as "Cannot find method (class)setHorizontalAlignment(string)". Can anyone kindly point me in the right direction?
Many thanks in advance
Most of the style attributes can be set with .setStyleAttributes().
.setStyleAttribute("fontSize","200%");
Note: Instead of writing font-size or margin-left, you need to use camelCase.
.setStyleAttributes({marginLeft:"15px",marginTop:"22px"});
These are just a few examples of how to use this.
In order to use horizontal alignment, you should use
.setHorizontalAlignment(UiApp.HorizontalAlignment.CENTER);
Use the autocomplete feature of Google Scripts to see all the options that you can use.
If you're already familiar with HTML, then consider using HTML templates to pull variables from the spreadsheet into the HTML. Templated HTML

Change a cells background color if value is over a certain number in google spreadsheet

This is sort of a two part questions.
First I need help writing this script, and then I need help with implementing a script in Google spreadsheets.
What I want to do is to change the background color of a cell (to red) if the number contained in that cell is over 40 (hours). For my spreadsheet I have 7 day columns, each with an in and an out column under them. I use a formula in the total column of the spreadsheet to subtract the value of the out time from the in time, and then add that number to the next cell until I get to the total. So that is the column that I want to apply this script to. I can write a little bit of JS, but I don't really know how to connect it to a spreadsheet instead of a website.
Here is my basic idea on how this should work:
//Trying to get a script that will change the background color to red if hours are over 40.
function onEdit(e) {
var cellID = e.source.getActiveCell();
var cellValue = e.cell.getValue(); //I may need to slice out the numbers before the first : for this to work..not sure b/c i cant get this to debug. Also not sure that I need this function, maybe can just do var cellValue = cellID.value or just use cellID.value ?
if (cellValue > 40 ) {
cellID.setBackgroundColor('red');
} else {
cellID.setBackgroundColor('white');
}
}
Not real sure about the onEdit function..would rather have it be my own function but I'm not sure what kind of listener to tie it to.
Also, if you guys can help me out with the general idea of this thing should work I can probably tweak it to make it work with my specific spreadsheet.
Really what I need to know is what i should be listening for on my spreadsheet, and how to actually attach the script to the spreadsheet.
Have you tried the conditional formatting which is in format menu.
i hope this will solve your problem.