How to access cell data from iframe Google Sheets - javascript

I'm trying to pull two specific numbers from a chart on a football website (Column PF, Row "Team Stats" and "Opp. Stats") and import them to my website as a JavaScript variable which I can do math, etc. with. My solution thus far since I don't want to get into anything too crazy has been use the IMPORTHTML function in Google Sheets, create a table, then export that bit of the spreadsheet using the "publish to web" feature which condenses it into an iframe.
I've tried using object properties of the iframe to access it including a getElementbyId dot property among others.
<h1>
<script>
var iframe = document.getElementById("data");
var elmnt = iframe.contentWindow.document;
//.getElementsByTagName("table")[0];
document.write(elmnt);
window.parent.document.getElementById('data')
</script>
</h1>
I was hoping for an output of the correct number but am getting [object HTMLDocument] or undefined depending on the properties I'm trying

Why don't you use the Sheets API ?
Using the api, you would use this simple command :
var result = Sheets.Spreadsheets.Values.get(spreadsheetId, range);
var numRows = result.values ? result.values.length : 0;
but the setup is a bit heavy
If you dont want to you can still serialize table data.
Here is the work of John Dyer :
function tableToJson(table) {
var data = []; // first row needs to be headers var headers = [];
for (var i=0; i<table.rows[0].cells.length; i++) {
headers[i] = table.rows[0].cells[i].innerHTML.toLowerCase().replace(/ /gi,'');
}
// go through cells
for (var i=1; i<table.rows.length; i++) {
var tableRow = table.rows[i]; var rowData = {};
for (var j=0; j<tableRow.cells.length; j++) {
rowData[ headers[j] ] = tableRow.cells[j].innerHTML;
} data.push(rowData);
}
return data;
}
the you'll can use data[column][row] to get the cell data;
In my opinion using the API is the best option in this specific case.

Related

Google Script return value from one sheet to another sheet

This is my first Google Script and I'm struggling a little bit while trying to bring back the values from a specific cell & sheet to another sheet.
I have a total of 18 columns, being the first one the ID which is going to be the input that the user would need to add in order to retrieve the data from one sheet to another. As the first one is the ID, and will be already be inputted by the user, I would need to retrieve the data from columns 2 to 18
Here is my code:
function SearchID() {
var columnIndex = 0;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formS = ss.getSheetByName("Manual Costs App"); // App sheet
var str = formS.getRange("D6").getValue(); // The ID to search will be inputted here
var dataS = ss.getSheetByName("Inputs").getDataRange().getValues(); // Retrieving the data based on the user input
for (var i = 0; i<=dataS.length; i++) {
var row = dataS[i];
if (row[columnIndex] == str) {
formS.getRange("D9").setValue(row[1]);
formS.getRange("D13").setValue(row[2]);
formS.getRange("D14").setValue(row[3]);
formS.getRange("D15").setValue(row[4]);
formS.getRange("D16").setValue(row[5]);
formS.getRange("D18").setValue(row[6]);
formS.getRange("D19").setValue(row[7]);
formS.getRange("D20").setValue(row[8]);
formS.getRange("D21").setValue(row[9]);
formS.getRange("D22").setValue(row[10]);
formS.getRange("D23").setValue(row[11]);
formS.getRange("D25").setValue(row[12]);
formS.getRange("D26").setValue(row[13]);
formS.getRange("D27").setValue(row[14]);
formS.getRange("D28").setValue(row[15]);
formS.getRange("D29").setValue(row[16]);
formS.getRange("D30").setValue(row[17]);
break;
}
}
}
The link to a sample spreadsheet of what I'm building is here
Update: Everything is fixed now! What I did was removing the space in the for loop. After that, it retrieved the data but a TypeError: Cannot read property '0'. Also solved it adding a break after the loop to avoid it.
It's a type in your for loop, notice how spelled length:
for (var i = 1; i <= values.lenght; i++)
You also don't want to set values line by line like you do, get a longer range and set the values with setValues() rather than setValue(). There's quite a bit of refactoring to do there actually.
The user did found the answer and updated it on the question.
Posting here as community wiki so it can be seen more clearly.
User's answer:
Update: Everything is fixed now! What I did was removing the space in the for loop. After that, it retrieved the data but a TypeError: Cannot read property '0'. Also solved it adding a break after the loop to avoid it.
function SearchID() {
var columnIndex = 0;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formS = ss.getSheetByName("Manual Costs App"); // App sheet
var str = formS.getRange("D6").getValue(); // The ID to search will be inputted here
var dataS = ss.getSheetByName("Inputs").getDataRange().getValues(); // Retrieving the data based on the user input
for (var i = 0; i<=dataS.length; i++) {
var row = dataS[i];
if (row[columnIndex] == str) {
formS.getRange("D9").setValue(row[1]);
formS.getRange("D13").setValue(row[2]);
formS.getRange("D14").setValue(row[3]);
formS.getRange("D15").setValue(row[4]);
formS.getRange("D16").setValue(row[5]);
formS.getRange("D18").setValue(row[6]);
formS.getRange("D19").setValue(row[7]);
formS.getRange("D20").setValue(row[8]);
formS.getRange("D21").setValue(row[9]);
formS.getRange("D22").setValue(row[10]);
formS.getRange("D23").setValue(row[11]);
formS.getRange("D25").setValue(row[12]);
formS.getRange("D26").setValue(row[13]);
formS.getRange("D27").setValue(row[14]);
formS.getRange("D28").setValue(row[15]);
formS.getRange("D29").setValue(row[16]);
formS.getRange("D30").setValue(row[17]);
break;
}
}
}

How to get nested elements in Google App script / JavaScript

I need to get values {nb_conversions":58"} under idgoal=ecommerceAbandonedCart, idgoal=4 and so on , I have tried different ways but unsuccessful, would appreciate it if someone can quickly sort this out for me.
Here is API URL: https://demo.matomo.cloud/?module=API&method=UserCountry.getRegion&idSite=1&period=day&date=yesterday&format=JSON&token_auth=anonymous
Sample Data:
[{"label":"Unknown","nb_uniq_visitors":558,"nb_visits":590,"nb_actions":980,"nb_users":0,"max_actions":14,"sum_visit_length":76439,"bounce_count":408,"nb_visits_converted":45,"goals":{"idgoal=ecommerceAbandonedCart":{"nb_conversions":58,"nb_visits_converted":58,"revenue":16199.299999999997,"items":88},"idgoal=ecommerceOrder":{"nb_conversions":10,"nb_visits_converted":10,"revenue":606.7,"revenue_subtotal":2448,"revenue_tax":0,"revenue_shipping":0,"revenue_discount":22.45,"items":12},"idgoal=4":{"nb_conversions":2,"nb_visits_converted":2,"revenue":2},"idgoal=5":{"nb_conversions":1,"nb_visits_converted":1,"revenue":5},"idgoal=6":{"nb_conversions":2,"nb_visits_converted":2,"revenue":4},"idgoal=7":{"nb_conversions":16,"nb_visits_converted":16,"revenue":16},"idgoal=8":{"nb_conversions":13,"nb_visits_converted":13,"revenue":0},"idgoal=10":{"nb_conversions":1,"nb_visits_converted":1,"revenue":0}},"nb_conversions":45,"revenue":633.7,"region":"xx","country":"xx","country_name":"Unknown","region_name":"Unknown","logo":"plugins\/Morpheus\/icons\/dist\/flags\/xx.png"}]
function Goals() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Goals");
var lastRow = sheet.getLastRow();
var today = sheet.getRange(1,6).getValue().toDateString();
var startDate = sheet.getRange(lastRow,1).getValue();
var now = sheet.getRange(lastRow,1).getValue().toDateString();
var dt = new Date(startDate);
//Logger.log(now);
// Logger.log(today);
//Logger.log(dt);
if( today != now){
var response = UrlFetchApp.fetch("https://demo.matomo.cloud/?module=API&method=UserCountry.getRegion&idSite=1&period=day&date=yesterday&format=JSON&token_auth=anonymous");
var json=response.getContentText();
var dataSet=JSON.parse(json);
for(var i = 0; i < dataSet.length; i++)
{
sheet.getRange(i+lastRow+1,2).setValue([dataSet[i]['label']]);
sheet.getRange(i+lastRow+1,3).setValue([dataSet[i]['nb_visits']]);
sheet.getRange(i+lastRow+1,4).setValue([dataSet[i]['nb_uniq_visitors']]);
sheet.getRange(i+lastRow+1,5).setValue([dataSet[i]['goals']['idgoal=ecommerceAbandonedCart']]);
sheet.getRange(i+lastRow+1,1).setValue(now);
}
dt.setDate(dt.getDate()+1);
lastRow = sheet.getLastRow();
sheet.getRange(lastRow+1,1).setValue(dt);
}
}
Given your sample data, try fetching the keys using Object.keys() and loop them one by one to get all nb_conversions instead of passing the idgoals individually. See below:
Code:
var dataSet = [{"label":"Unknown","nb_uniq_visitors":558,"nb_visits":590,"nb_actions":980,"nb_users":0,"max_actions":14,"sum_visit_length":76439,"bounce_count":408,"nb_visits_converted":45,"goals":{"idgoal=ecommerceAbandonedCart":{"nb_conversions":58,"nb_visits_converted":58,"revenue":16199.299999999997,"items":88},"idgoal=ecommerceOrder":{"nb_conversions":10,"nb_visits_converted":10,"revenue":606.7,"revenue_subtotal":2448,"revenue_tax":0,"revenue_shipping":0,"revenue_discount":22.45,"items":12},"idgoal=4":{"nb_conversions":2,"nb_visits_converted":2,"revenue":2},"idgoal=5":{"nb_conversions":1,"nb_visits_converted":1,"revenue":5},"idgoal=6":{"nb_conversions":2,"nb_visits_converted":2,"revenue":4},"idgoal=7":{"nb_conversions":16,"nb_visits_converted":16,"revenue":16},"idgoal=8":{"nb_conversions":13,"nb_visits_converted":13,"revenue":0},"idgoal=10":{"nb_conversions":1,"nb_visits_converted":1,"revenue":0}},"nb_conversions":45,"revenue":633.7,"region":"xx","country":"xx","country_name":"Unknown","region_name":"Unknown","logo":"plugins\/Morpheus\/icons\/dist\/flags\/xx.png"}];
for (var i = 0; i < dataSet.length; i++) {
// Upon testing your linked data, some of them doesn't have goals key.
// Be sure to only access those who have goals to avoid encountering an error.
if(dataSet[i]['goals']) {
Object.keys(dataSet[i]['goals']).forEach(function (idgoal){
Logger.log(dataSet[i]['goals'][idgoal]['nb_conversions']);
});
}
}
Output:
Note:
I recommend you format the data or view it in a site like this one. It really helps a lot.
The nb_conversions being fetched above are only the ones under the goals key.
If some idgoals doesnt have nb_conversions, just skip them like in goals above. The linked data above all has nb_conversions so I didn't include them for now.
References
Object.keys()

Google Apps Script - XML Parser - Regex

I am using a Google Apps Script that pulls the content from a feed in a sheet.
This is the code that I'm using:
function processXML(FeedURL,sheetsFileDestinationURL,rawPasteSheetName,OPT_childNamesArray,OPT_Namespace){
var OPT_childNamesArray = ["link"]; // get only item url from the feed
var GoogleSheetsFile = SpreadsheetApp.openByUrl(sheetsFileDestinationURL);
var GoogleSheetsPastePage = GoogleSheetsFile.getSheetByName(rawPasteSheetName);
if (OPT_childNamesArray){
GoogleSheetsPastePage.getDataRange().offset(1,0).clearContent(); // get all filled cells, omitting the header row, and clear content
}
else {
GoogleSheetsPastePage.getDataRange().offset(0,0).clearContent(); // get all filled cells, INCLUDING the header row, and clear content
}
// Generate 2d/md array / rows export based on requested columns and feed
var exportRows = []; // hold all the rows that are generated to be pasted into the sheet
var XMLFeedURL = FeedURL;
var feedContent = UrlFetchApp.fetch(XMLFeedURL).getContentText(); // get the full feed content
var feedItems = XmlService.parse(feedContent).getRootElement().getChild('channel').getChildren('item'); // get all items in the feed
for (var x=0; x<feedItems.length; x++){
// Iterate through items in the XML/RSS feed
var currentFeedItem = feedItems[x];
var singleItemArray = []; // use to hold all the values for this single item/row
// Parse for specific children (requires names and namespace)
if (OPT_childNamesArray){
for (var y=0; y<OPT_childNamesArray.length; y++){
// Iterate through requested children by name and fill rows
var currentChildName = OPT_childNamesArray[y];
if (OPT_Namespace){
if (currentFeedItem.getChild(OPT_childNamesArray[y],OPT_Namespace)){
singleItemArray.push(currentFeedItem.getChildText(OPT_childNamesArray[y],OPT_Namespace));
}
else {
singleItemArray.push("null");
}
}
else {
if (currentFeedItem.getChild(OPT_childNamesArray[y])){
singleItemArray.push(currentFeedItem.getChildText(OPT_childNamesArray[y]));
}
else {
singleItemArray.push("null");
}
}
}
exportRows.push(singleItemArray);
}
// Parse for ALL children, does not require knowing names or namespace
else if (!OPT_childNamesArray){
var allChildren = currentFeedItem.getChildren();
if (x == 0){
// if looking at first item, create a header row first with column headings
var headerRow = [];
for (var h=0; h<allChildren.length; h++){
headerRow.push(allChildren[h].getName());
}
exportRows.push(headerRow);
}
for (var c=0; c<allChildren.length; c++){
singleItemArray.push(allChildren[c].getText());
}
exportRows.push(singleItemArray);
}
}
// Paste the generated md array export into the spreadsheet
if (OPT_childNamesArray){
GoogleSheetsPastePage.getRange(2,1,exportRows.length,exportRows[1].length).setValues(exportRows);
}
else if (!OPT_childNamesArray){
var maxRangeLength = 0;
var currentRowIndex = 1;
for (var x = 0; x<exportRows.length; x++){
if (exportRows[x].length > maxRangeLength){
maxRangeLength = exportRows[x].length;
}
GoogleSheetsPastePage.getRange(currentRowIndex,1,1,exportRows[x].length).setValues([exportRows[x]]);
currentRowIndex++;
}
}
}
My problem is this:
When I run this code I get:
https://url/115-396/
https://url/115-396/
https://url/115-396/
I need to remove "115-396/".
So I tryed to add this code but didn't work:
...
// Paste the generated md array export into the spreadsheet
if (OPT_childNamesArray){
for (var k = 0; k < exportRows.length; k++) {
var re = '115-396/'
var replacingItem = '';
var URL = exportRows[0].toString().replace(re, replacingItem);
}
GoogleSheetsPastePage.getRange(2,1,exportRows.length,exportRows[1].length).setValue(URL);
}
else if (!OPT_childNamesArray){
...
Edit after #Yuri reply:
// Paste the generated md array export into the spreadsheet
if (OPT_childNamesArray){
for ( k=0; k < exportRows[0].length; k++) {
var re = '115-396/'
var replacingItem = '';
exportRows[0][k] = exportRows[0][k].toString().replace(re, replacingItem);
}
GoogleSheetsPastePage.getRange(2,1,exportRows.length,exportRows[1].length).setValues(exportRows);
}
result:
https://url/
https://url/115-396/
https://url/115-396/
Basically, the regex is applied only to the first url.
How I can make that the regex is applied to all the url's?
Any help?
Thanks
You are using a for to iterate thru the exportRow array, but later on, you're not using the k iterator inside the for.
Then, you are not accessing the exportRows array, only the first position:
var URL = exportRows[0].toString().replace(re, replacingItem);
Shouldn't be?
var URL = exportRows[k].toString().replace(re, replacingItem);
In that case, it won't work, because URL it's not an array, so by doing this you are only saving the last assignation produced on the for iterator on the URL, I believe you are trying to do the following:
for ( k=0; k < exportRows.length; k++) {
var re = '115-396/'
var replacingItem = '';
exportRows[k] = exportRows[k].toString().replace(re, replacingItem);
}
And you'll have exportRows as an array of the desired url's without the 115-396 extensions.
Now you can place this on the spreadsheet with setValue as you were doing, but setValue is for strings, integers, etc, and not for arrays. For arrays you have setValues()
GoogleSheetsPastePage.getRange(2,1,exportRows.length,exportRows[1].length).setValues(exportRows);
But, then, the range of exportRows should match the range of your getRange selection, which I'm not sure it's happening.
Just to clarify it, exportRows.length is the length of the array, and exportRows[1] is the length of the string/url stored on the position 1 of the array.
Hope this helps, the question is not really clear neither the intentions, provide more info if still not working.
How to know the size of the range you're getting?
var myrange = GoogleSheetsPastePage.getRange(2,1,exportRows.length,exportRows[1].length)
Logger.log(myrange.getNumRows());
Logger.log(myrange.getNumColumns());
You'll be able to know the range you have on getRange and make it match with the exportRows size.
Make sure to check the attached documentation, and in case you have more doubts please open a new question related to it.

Google apps script - using for loop to pull data from range based on condition

I'm having an issue pulling the correct values out of a for loop in Google Sheets.
Here's my code:
Note: this is a snippet from a larger function
function sendEmails() {
var trackOriginSheet = SpreadsheetApp.getActiveSpreadsheet().getName();
var getMirSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Miranda");
//Set a new object to hold conditional data
var holdingData = new Object();
//Create function to get values from origin sheet
var returnedValues = function (trackOriginSheet) {
//Load dynamic variables into an object via returnedValues()
if (trackOriginSheet === getMirSheet) {
var startMirRow = 2; // First row of data to process
var numRowsMir = 506; // Number of rows to process
// Fetch the range of cells A2:Z506
var dataRangeMir = getMirSheet.getRange(startMirRow, 1, numRowsMir, 26);
// Fetch values for each cell in the Range.
var dataMir = dataRangeMir.getValues();
for (var k in dataMir) {
var secondRowMir = dataMir[k];
var intRefDescMir = secondRowMir[3];
var intAdminActionsMir = secondRowMir[4];
//Push returned data to holdingData Object
holdingData.selectedData = secondRowMir;
holdingData.refDesc = intRefDescMir;
holdingData.adminActions = intAdminActionsMir;
}
}
}
Here's a copy of the sheet I'm working on
What I need to have happened here first, is track the origin sheet, then create an object to hold data returned from the returnedValues() function. Later, I'll call the properties of this object into a send email function.
The problem is that I need to be able to pull data from the selected sheet dynamically (the "Miranda" sheet in this case.) In other words, when a user selects the "Yes" option in column I of the Miranda sheet, the first thing this script needs to do is pull the values of the variables at the top of the for loop within the same row that the user selected "Yes." Then, I'm pushing that data to a custom object to be called later.
It's apparent to me, that I'm doing it wrong. There's, at least, something wrong with my loop. What have I done? :)
EDIT:
After reviewing the suggestion by VyTautas, here's my attempt at a working loop:
for (var k = 0; k < dataMir.length; k++) {
var mirColI = dataMir[k][8];
var mirRefDesc = dataMir[k][2];
var mirAdminActions = dataMir[k][3];
var mirDates = dataMir[k][4];
if (mirColI === "Yes") {
var activeRowMir = mirColI.getActiveSelection.getRowIndex();
//Pull selected values from the active row when Yes is selected
var mirRefDescRange = getMirSheet.getRange(activeRowMir, mirRefDesc);
var mirRefDescValues = mirRefDescRange.getValues();
var mirAdminActionsRange = getMirSheet.getRange(activeRowMir, mirAdminActions);
var mirAdminActionsValues = mirAdminActionsRange.getValues();
var mirDatesRange = getMirSheet.getRange(activeRowMir, mirDates);
var mirDatesValues = mirAdminActionsRange.getValues();
var mirHoldingArray = [mirRefDescValues, mirAdminActionsValues, mirDatesValues];
//Push mirHoldingArray values to holdingData
holdingData.refDesc = mirHoldingArray[0];
holdingData.adminActions = mirHoldingArray[1];
holdingData.dates = mirHoldingArray[2];
}
}
Where did all that whitespace go in the actual script editor? :D
You already correctly use .getValues() to pull the entire table into an array. What you need to do now is have a for loop go through dataMir[k][8] and simply fetch the data if dataMir[k][8] === 'Yes'. I also feel that it's not quite necessary to use for (var k in dataMir) as for (var k = 0; k < dataMir.length; k++) is a lot cleaner and you have a for loop that guarantees control (though that's probably more a preference thing).
You can also reduce the number of variables you use by having
holdingData.selectedData = mirData[k]
holdingData.refDesc = mirData[k][2] //I assume you want the 3rd column for this variable, not the 4th
holdingData.adminActions = mirData[k][3] //same as above
remember, that the array starts with 0, so if you mirData[k][0] is column A, mirData[k][1] is column B and so on.
EDIT: what you wrote in your edits seems like doubling down on the code. You already have the data, but you are trying to pull it again and some variables you use should give you an error. I will cut the code from the if, although I don't really see why you need to both get the active sheet and sheet by name. If you know the name will be constant, then just always get the correct sheet by name (or index) thus eliminating the possibility of working with the wrong sheet.
var titleMirRows = 1; // First row of data to process
var numRowsMir = getMirSheet.getLastRow(); // Number of rows to process
// Fetch the range of cells A2:Z506
var dataRangeMir = getMirSheet.getRange(titleMirRows + 1, 1, numRowsMir - titleMirRows, 26); // might need adjusting but now it will only get as many rows as there is data, you can do the same for columns too
// Fetch values for each cell in the Range.
var dataMir = dataRangeMir.getValues();
for (var k = 0; k < dataMir.length; k++) {
if (dataMir[k][7] === 'Yes') { //I assume you meant column i
holdingData.refDesc = dataMir[k] //this will store the entire row
holdingData.adminActions = dataMir[k][3] //this stores column D
holdingData.dates = dataMir[k][4] //stores column E
}
}
Double check if the columns I have added to those variables are what you want. As I understood the object stores the entire row array, the value in column called Administrative Actions and the value in column Dates/Periods if Applicable. If not please adjust accordingly, but as you can see, we minimize the work we do with the sheet itself by simply manipulating the entire data array. Always make as few calls to Google Services as possible.

How can I output the contents of a scripted array in a google sheets?

I'm a totally new to coding and Google App script, so I humbly ask my question.
I have look all over the place for an answer to this problem and can't seem to find a way to solve it.
Below is my code that gets data from Google Sheets based on their IDs. I can bring the data in and add for 4 sheets and from a particular range.
The script successfully stores the data in an array(arr).
The script sends the data the size of the original import range to the active sheet.
But, how can I output all of the data I can see logged in the array (arr)?
I've tried changing the range size of the output, but to no avail, it just causes and error.
function GetData() {
var classes = ["1XN9NPs_irdpVBEuy0saY-m_PjMW8BxIOgvLkaekdMXo",
"1XN9NPs_irdpVBEuy0saY-m_PjMW8BxIOgvLkaekdMXo",
"11Pdsixi17YjtePg4L780mtGkElWU7TUmshYtJnzxbMU",
"11y8ILVN3lTCWaQYr0nhLdDdLsDDoRqcR2BHz42leUbI"];
var ui = SpreadsheetApp.getUi();
for (var j = 0; j < classes.length; j++) {
var exSs = SpreadsheetApp.openById(classes[j]);
var exSheet = exSs.getSheetByName("tastodolist");
var range = "A4:F10"
var exRange = exSheet.getRange(range);
var values = exRange.getValues();
var arr = [];
arr.push(values);
for (var i = 0; i < arr.length; i++) {
//The log contains all the data that is needed.
Logger.log(arr[i]);
}
//I'm struggling to get all of the logged data into the active
sheet - my code can only output 7 rows of data.
SpreadsheetApp.getActiveSheet().getRange(1,1,7,6).setValues(arr[i]);
}
}
You're only getting 7 rows and 6 columns of the sheet, get all the array's size to output it entirely:
SpreadsheetApp.getActiveSheet().getRange(1,1,arr.length,arr[0].length).setValues(arr[i]);
Also, this is primarily a [google-apps-script] question.

Categories