I can access a range of cells from the default first sheet using:
var url = 'https://docs.google.com/spreadsheets/d/' + spreadsheet_id + '/gviz/tq?range=C3:C100';
$.ajax(url).done(function(result){
....
But this only gives the first sheet. How can I access the other sheets in the spreadsheet? I would think it's another parameter option at the end of the url (where it says "?range=C3:C100")
Using A1 notation like Sheet2!A1:B2 or 'my sheet'!A1:B2 in the range parameter works. Using the the spreadsheet used in mapbox's sheets integration demo, ...tq?range=C3:C4 yields the same result as ...tq?range=Demo!C3:C4. However, these results are in a custom mime type. For pure JSON, use the the sheets api v4 value GET endpoint : https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}/values/{range} (docs here). I'd highly recommend their API explorer, which shows exactly the results you would get from a query.
Related
I have a Google Sheet which is being populated by a Google Form. I am using Google Apps Script to add some extra functionality. Please feel free to access and modify these as needed in order to help.
My goal is to be able to take responses from a Google Form, have that come into a Google Sheet, and then, using a template file, populate the template with the responses and have that sent out as an email attachment to whomever I specify.
This is a dumbed down version of my ultimate project, but the process should remain the same so that I can transpose from this small scale example to that bigger one.
Currently, in my Apps Script, I have code which is successfully able to make a copy of the template file and name it accordingly:
//Enter collected info into Requirements Template
const googleSheetTemplate = DriveApp.getFileById('1wqCwMhpuDLReU1hE1CbcDL-Vdw_4zge1xM6oOl34Ohg');
const destinationFolder = DriveApp.getFolderById('1GxNZQmP8mxHBhVl5AMoqBFs8sAIYzcm3');
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Form Responses 2');
const copy = googleSheetTemplate.makeCopy(`${row[3]}, ${row[0]} Vehicle Order` , destinationFolder);
const newSheet = SpreadsheetApp.openById(copy.getId());
const A1 = newSheet.getActiveRange();
But the next few lines which are meant to be able to find and replace certain strings within the newly copied Sheet does not seem to function properly.
A1.createTextFinder("{{Customer}}").replaceAllWith(row[3]);
A1.createTextFinder("{{Car}}").replaceAllWith(row[1]);
A1.createTextFinder("{{Color}}").replaceAllWith(row[2]);
A1.createTextFinder("{{Delivery}}").replaceAllWith(row[5]);
I just get the same dummy lines back in the new copy.
I was following another post which I found on a different issue but with the same goal. The majority of the concept I was looking to copy came from this blog post, but where they used a Google Doc, I was hoping to use a Google Sheet. Ultimately, the person that receives this new sheet will need to do some calculations and things with the provided information, so it still needs to be in Sheet form.
What modifications do I need to make in order to successfully replace the text in the template with the answers provided from the Google Form responses?
As far as I can tell, this line means some cells on the sheet were selected:
const A1 = newSheet.getActiveRange();
And script will search and replace within these cells only.
Probably you need to define the range with no reference on active range. Something like this:
const A1 = newSheet.getRange("A1:A"); // or "A1:Z", or .getDataRange()
I don't know what the range you need.
From the documentation, createTextFinder(findText) only works on a Sheet class, this means you need to define a Sheet variable before replacing text:
const copy = googleSheetTemplate.makeCopy(`${row[3]}, ${row[0]} Vehicle Order` , destinationFolder);
const newSheet = SpreadsheetApp.openById(copy.getId()).getSheets()[0];
newSheet.createTextFinder("{{Customer}}").replaceAllWith(row[3]);
newSheet.createTextFinder("{{Car}}").replaceAllWith(row[1]);
newSheet.createTextFinder("{{Color}}").replaceAllWith(row[2]);
newSheet.createTextFinder("{{Delivery}}").replaceAllWith(row[5]);
I'm writing an AngularJS application which should perform image search through entire web using google custom search api.
I've used the query as follows:
'https://www.googleapis.com/customsearch/v1?key='+ googleApiKey + '&cx=014766414461901118935:knbecyxzx4u&searchType=image&startIndex=41&imgType=photo&q=' + query;
As a result I get an array which consists of 10 elements. The documentation says that it can return up to the first 100 results. How can I load all the rest of results beside those 10 which come from the query above?
Note: Besides the array of images I also get the query object which has the nextPage object which holds the metadata describing the query to use for the next page of results, but apparently I have no idea how to use it as the startIndex value is always 11
I believe that you use &start instead of &startIndex
I’m trying to use data from multiple GoogleSheets to produce a single HighChart graph.
I’d like to do this without moving all the data into one area of a single spreadsheet, particularly as I want to use the drilldown option which would make it difficult to collect all the data together.
I thought I could pass the columns as an array and then reference the array in the data property of my chart, but I’m struggling to do that with even one column from one sheet.
I have searched for answers online, but I have not found anything relating to highcharts getting data from multiple sources.
Previous Research:
Using GoogleSheets Data as an array before creating the chart: (Removed Link) - The problem is that I could only use one GoogleSheets reference here as the chart object sits inside the data object.
API documentation - (Removed Link) – tells me I can access the columns but that’s not the part I’m having problems with
Querying the Chart: (Removed Link) - I have actually considered making hidden charts, then interrogating the data and making new charts from those, but that seems like a very long way round and I’m still not sure I could grab all the data I need.
Using two GoogleSheets for separate charts on the same page: (Removed Link) I have done this.
Please could you help me to understand how I can access the properties and methods of this object outside of the object itself?
Thank you.
My Code:
//Function to produce a data array ***Not Working - Cannot extract array from object method***
function getData(){
Highcharts.data({
googleSpreadsheetKey: '12x66_QEkTKg_UzvpHEygdTmfnu7oH81pSQYn78Hxt80',
googleSpreadsheetWorksheet: 4,
startColumn: 16,
endColumn: 22,
startRow: 63,
endRow: 76,
parsed: function (columns) {
var dataTest2 = [];
var columnLength = columns[1].length;
for (i = 0; i < columnLength; i = i + 1) {
dataTest2.push(columns[1][i]);
}
alert(dataTest2); //This works here, but not if I move it outside of the method, even if I return it.
var testReturn = this.googleSpreadsheetKey; //trying to return any property using "this" - I've also tried this.googleSpreadsheetKey.value but still undefined
return testReturn; //returns "Undefined"
}
});
}
You could use Google Sheets webQuery. Basically, this is a method to export the Spreadsheet's data into a given format such as csv, json, etc. In your case, the link should look like this:
https://docs.google.com/spreadsheet/tq?key=12x66_QEkTKg_UzvpHEygdTmfnu7oH81pSQYn78Hxt80&gid=4&tq=select%20A,%20B&tqx=reqId:1;out:csv;%20responseHandler:webQuery
Please note that here "tg?key" is the key of your Google Sheet, and "&gid=" is NOT 4, this only tells Highcharts to selected Sheet 4, but for Google Sheets look at the source link and copy the numbers which go after "&gid=". Furthermore, "&tq=" is used to select the columns of the Google Sheet, which in the link above selects "Column A" and "Column B". To find out more on how to select columns and query the output refer to:
https://developers.google.com/chart/interactive/docs/querylanguage?csw=1#Setting_the_Query_in_the_Data_Source_URL
Lastly, "&tqx=" is used to output your data in the format you want. The above link uses "out:csv" which will output the data as comma-separated values. This could as well be Json if you like. Have a look at this documentation:
https://developers.google.com/chart/interactive/docs/dev/implementing_data_source#requestformat
In order to implement this output into your javascript code which you would then use to built your chart, you could use external Javascript libraries to handle these outputs. If you output your data as CSV, you can use "papaparse.js" to parse the CSV into Json which you can be then read by highcharts and allows you to built the chart. Refer to this documentation:
http://papaparse.com/docs#remote-files
An alternative to this would be, to output your Google Sheets directly as Json, then use jquery to make an Ajax Call and load the JSON-encoded data into your Javascript code. Precisely, you could perhaps use jQuery.getJSON() to get the data. Look at this link for more details on how to get Json:
http://api.jquery.com/jquery.getjson/
Finally, it is up to you on which format you choose to output the data, I prefer using Json as it saves you the extra step of having to convert the CSV into Json. Whatever suits you best and is easier for you to understand.
Once you have your data, you may have to parse your Json objects with Json.parse(), and then organize your data into an array with .push(). As #jlbriggs stated, organize your data first before you built the chart. Afterwards, you can make two, three or more Ajax calls to import data from different sources. I would not use many as this will impact in your loading and slow down data transfer.
NB: It is important to format the data accordingly for Highcharts to recognize the data, so use parseFloat() to convert strings into numbers, Date.UTC() to convert strings into date, etc.
Hope this helps you.
I need a JavaScript method for my webpage to count how many rows are in a Google sheet (it's used as a response sheet for a form). I've been scouring the web for easy tutorials on how to make Google Sheets into a database.
Is there a simpler way to do it?
I don't have much experience in Google scripts, but all I need is a way I can have read access to the spreadsheet using preferably Ajax or some similar JavaScript method.
To retrieve the number of rows in your Google Spreadsheet use the following:
var ss = SpreadsheetApp.openById("1qNCf0wKl................");
var sheet = ss.getSheetByName("sheet_name");
var number = sheet.getMaxRows().toString();
var number = number.replace(".0","");
Logger.log(number);
In order to have read access to a Spreadsheet the guidance provided in the following link is good enough:
Query Google Spreadsheet with URL Parameters
For example, if you want to get all the cells with the word «budget» in your Spreadsheet, use the following script:
var id = "1qNCf0wKlx1RF......";
var column = "A";
var query = "budget";
var url = "https://docs.google.com/spreadsheets/d/"+id+"/gviz/tq?tq=SELECT%20*%20where%20"+column+"%20contains%20%22"+query+"%22";
var text = UrlFetchApp.fetch(url).getContentText().toString();
Logger.log(text);
For this, the Spreadsheet has to be published previously. After retrieving the «budget» rows the text has to be formated, but that's another issue.
Here is what you can do
Write a simple Apps script using ContentService that will return a JSON with the number of rows in the spreadsheet (and any other info you need). To get the number of rows, you can use the getLastRow() function
Publish your script as a service(web app) and make an AJAX call from your JS code.
I'm trying to create a custom function in my google spreadsheet with the script editor.
I can't seem to allow the user to give the function an array as the argument.
My script works when I hardcode like this:
var values = SpreadsheetApp.getActiveSheet().getRange("G2:j30").getValues();
What I want to do is pass the array as an argument:
function arrayToList(chosenArray) {
...
var values = SpreadsheetApp.getActiveSheet().getRange(chosenArray).getValues();
...
}
Now this functionality is not implemented in GAS. There are similar questions in SO (here and here). You can post a feature request to the issue tracker.
Riku - Did you try calling your function as arrayToList(G2:j30)? I believe Apps Script will convert that selected range to a comma separated string (going left -> right and then top->down).
It wont be the same as a the Range object, but at least you can get a CSV string representation of the selected range and perhaps that is sufficient?
There are two ways to pass an array as argument to a Google Sheets custom function
By using a range reference, like A1:D5. Usage: =myCustomFunction(A1:D5).
By using the Google Sheets array notation like {1,2;"Yellow","Orange"}. Usage: =myCustomFunction({1,2;"Yellow","Orange"}).
Note: If your spreadsheet is set to use comma as decimal separator use / as column separator
References
https://developers.google.com/apps-script/guides/sheets/functions
Using arrays in Google Sheets