I already have a spreadsheet that submitted by 3rd party apps (which I can't change its code).
Then, I need to send HTTP (Post) automatically to http://THISISMYURL.url/go.aspx?fieldone=xxx&fieldtwo=xxx when my Google spreadsheet updated.
What in my mind is to create a script in Google Spreadsheet and use UrlFetchApp in my code.
function SendHTTPpost() {
//How to pick data from any field in last column (everytime google spreadsheet updated)?
var data =
{
"fieldOne" : "value for field one (from last column)",
"fieldTwo" : "value for field two (from last column)",
};
var options =
{
"method" : "post",
"payload" : data
};
UrlFetchApp.fetch("http://THISISMYURL.url/go.aspx", options);
}
I know that I missed some command which to fetch some data from spreadsheet. Anybody get some idea to do this?
Thanks for helping me :)
Thanks to #Mogsdad for giving a clue for me. And thanks to #Zig Mandel for suggesting me to read spreadsheetApp documentation.
Here are what I did in my code, and it's works!
// get the spreadsheet object
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
// set the first sheet as active
SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[0]);
// fetch this sheet
var sheet = spreadsheet.getActiveSheet();
// figure out what the last row is
var lastRow = sheet.getLastRow();
var lastColumn = sheet.getLastColumn();
At the last command, I use UrlFetchApp classes by fetch(url) method that I learn from here
Thanks to all for helping me.
And hopefully it will help others too.
Yeay!!!
Related
I've been having problems with Highcharts when requesting from a google sheet, few days ago it was all good, but suddenly it was throwing an error 404, and I don't know what to do. Higcharts only ask you to put the google spreadsheet key, and the number of the tab, and nothing else. I want to change the URL that Higcharts makes the request but I don't know how.
Error:
data.src.js:124 GET https://spreadsheets.google.com/feeds/cells/1YtSfF3-9kzx1G6zwllLX9GhkVgufSUjQyKVI3Ken-AA/2/public/values?alt=json 404
It seems like a google issue: https://support.google.com/docs/thread/121088347/retrieving-data-from-sheets-results-in-404-error-50-of-the-time
feeds is the older version of Sheets API and it's shut down.
See Google's announcement here https://cloud.google.com/blog/products/g-suite/migrate-your-apps-use-latest-sheets-api
Try tu use this function to get values as json
function getJson(id,gid){
var txt = UrlFetchApp.fetch(`https://docs.google.com/spreadsheets/d/${id}/gviz/tq?tqx=out:json&tq&gid=${gid}`).getContentText();
var jsonString = txt.match(/(?<="table":).*(?=}\);)/g)[0]
var json = JSON.parse(jsonString)
return(json)
}
with id of the spreadsheet and gid of your sheet (may be 0)
note that first row is dedicated to labels, then rows[0] corresponds to row#2 and c[0] corresponds to column A
if you want to retrieve B1 (column 2 row 1) : json.cols[1].label
if you want to retrieve B2 (column 2 row 2) : json.rows[0].c[1].v
I am trying to move my team's productivity to another sheet via Apps Script.
On the sheet "Prod" you can see the weekly productivity the team has put in. The "Approved" column has checkboxes where the work can be manually verified.
The second sheet is the "Archives" sheet. Where the Apps Script will move all of the responses from the "Prod" sheet over there.
Apps Script Code
function archive() {
// Get the information
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName('Prod');
var a = ss.getSheetByName('Archives');
var data = s.getRange('A2:F').getValues();
// Archive the results
for (i in data) {
var write = data[i];
a.appendRow(write);
}
// Clear the results
s.getRange('A2:E').clear();
s.getRange('F2:F').uncheck();
}
The Problem
The code works perfectly fine, however, when it is moving the "Prod" responses over to the "Archives" it includes the empty rows. This is because in the "Approved" column the checkbox is technically false, which means it's included. How would I go about changing the code to only transfer rows that have data in the A column?
Thanks!
I believe your goal as follows.
You want to copy the values from Prod to Archives when the value of column "A" in Prod is not empty.
Modification points:
In your script, all values of var data = s.getRange('A2:F').getValues(); is copied to Archives. So in this case, it is required to check the column "A" of data.
When appendRow() is used in a loop, the process cost will be high. Ref
When above points are reflected to your script, it becomes as follows.
Modified script:
function archive() {
// Get the information
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName('Prod');
var a = ss.getSheetByName('Archives');
var data = s.getRange('A2:F').getValues();
// Archive the results <--- I modified this part.
var copyValues = data.filter(([a]) => a.toString() != "");
a.getRange(a.getLastRow() + 1, 1, copyValues.length, copyValues[0].length).setValues(copyValues);
// Clear the results
s.getRange('A2:E').clear();
s.getRange('F2:F').uncheck();
}
References:
Benchmark: Reading and Writing Spreadsheet using Google Apps Script
filter()
setValues(values)
I am trying to set up google sheets to pull data from an API. At first it worked, now it just says "Loading error..." on the cell with the formula.
I have tried it on a new sheet. I have simplified it to one input variable.
function getPrice(item,city){
var url = "https://www.albion-online-data.com/api/v1/stats/prices/"+item+"?
locations="+city;
var jsondata = UrlFetchApp.fetch(url);
var object = JSON.parse(jsondata.getContentText());
return object[0].sell_price_min;
}
The variables are "item" and "city". For which examples include "T1_Wood" item in "Caerleon" city. I'd like to make a table to pull the prices for different items in different cities. Thank you!
I'm trying to connect Google Sheets to JIRA to gather the data for updating reports automatically.
I'm struggling however with a couple of points in this modified script.
I want to return the component field but calling the name field returns undefined.
var components = data["issues"][id].fields.components.name;
If I remove the name field, then I get the following response:
{name=#####, self=https://www.#########/rest/api/2/component/26357, id=26357}
The second issue is that only a handful of issues are being rendered. As far as I can see my REST call looks OK, as does the writing to tables:
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); //select active spreadsheet
sheet.getRange(2, 1, issuessss.length, 7).setValues(issuessss); // write from cell A2
break;
Anyone have any ideas that could help?
The field component is an array so components.name does not exist (i.e components[0].name would work).
Before setting the values, try to execute a flush first.
I was trying to make a script for a google spreadsheet that would remove duplicate entries that were submitted via a google form. Essentially the google form writes the entries to the google spreadsheet and I wanted any google form entry that were a duplicate to an entry already submitted through the form, to be removed automatically. This is the code that I have now:
function onFormSubmit(e) {
var sheet = SpreadsheetApp.openById("13ggBeSGGxhI291uPcIr0RudwxxKUigtNEN750Q2hCBM");
var data = sheet.getDataRange().getValues();
var newData = new Array();
for(i in data){
var row = data[i];
var duplicate = false;
for(j in newData){
if(row.join() == newData[j].join()){
duplicate = true;
}
}
if(!duplicate){
newData.push(row);
}
}
sheet.clearContents();
sheet.getRange(1, 1, newData.length, newData[0].length).setValues(newData);
}
I know parts of it are still incorrect so i was hoping someone could help me get a better idea of what I'm doing wrong so that I can make the necessary corrections. Thanks!
You opened a Spreadsheet object, you also need to open the actual sheet (One spreadsheets has one or more sheets).
The sheet class has a method clearContents(), a spreadsheet doesn't.
Try SpreadsheetApp.openById("13ggBeSGGxhI291uPcIr0RudwxxKUigtNEN750Q2hCBM").getSheetByName(whateverYourResponseSheetNameIsAsAString);.
As for why the Spreadsheet class supports getValues() but not clearContents(), I don't know, it seems inconsistent. For the former it is implicitly running getActiveSheet().