I'm in need of some help with the final part of my code. I'm working with an API to get the name of the street and city from the government database and i use the Zip code and house number as input. The API i have working, but the final step is to pass cell values (Zip code, house number) as parameters into my formula. I cannot seem to get this working.
Here is my code:
function postHuisNaarStraat (ref1, ref2){
var PCCell = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(ref1).getValue();
var HNCell = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(ref2).getValue();
var postcode = "filters[postcode]=" + PCCell;
var huisnummer = "filters[huisnummer]=" + HNCell;
var API_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
var url = "https://api.overheid.io/bag?" + postcode + "&" + huisnummer;
var response = UrlFetchApp.fetch(
url,
{
"headers":{
"ovio-api-key":API_KEY,
"Accept":"application/hal+json",
"Content-Type":"application/json"
}
}
);
// parse JSON reply
var json = response.getContentText();
var data = JSON.parse(json);
// Logger.log(data);
Logger.log(data["_embedded"]["adres"][0]["openbareruimte"]);
}
Please help me get the data from my cells (postcode, housenumber) and add their respective value in my formula.
Here is what i want to do but cant get working
Replace
var PCCell = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(ref1).getValue();
var HNCell = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(ref2).getValue();
by
var PCCell = ref1;
var HNCell = ref2;
or even better (as this will make your code shorter), instead of ref1 and ref2 as function argument names use PCCell and HNCell, respectivelly.
Resources
https://developers.google.com/apps-script/guides/sheets
I am trying to insert a formatted signature and a number in separate rows at end of a spreadsheet. I have found that I can easily append the values I want however I cannot format the signature variable. Any help would be appreciated.
function signNote(){
//gets and signs for current user
var email = Session.getEffectiveUser().getEmail()
var self = ContactsApp.getContact(email)
if(self){
// gets full name
var name = self.getFullName();
//gets Clinician Profile Information
var clinicianProfile = SpreadsheetApp.openById('idGoeshere')
var clinicianInfoFile = clinicianProfile.getSheetByName(name)
var license = clinicianInfoFile.getRange('D5').getValue()
var signature = clinicianInfoFile.getRange('D6').getValue()
//Fills in signature and license
var noteSign = SpreadsheetApp.getActiveSheet()
var insertSignatureLign = noteSign.insertRowAfter(noteSign.getLastRow())
var insertSignature = insertSignatureLign.getDataRange()
noteSign.appendRow(['Signature' , signature])
noteSign.appendRow(['License Number' , license])
I am trying to set up a script which can push data from an App Script into a Google Sheet.
I have the script successfully logging what I want, which goes in the following format Account budget is 12344, but now I want to push this into a Google Sheet. I have set up a variable containing the URL and another variable containing the sheet name, and also a clear method to delete anything already there.
Find the code I have below:
// - The link to the URL
var SPREADSHEET_URL = 'abcdefghijkl'
// - The name of the sheet to write the data
var SHEET_NAME = 'Google';
// No to be changed
function main() {
var spreadsheet = SpreadsheetApp.openByUrl(SPREADSHEET_URL);
var sheet = spreadsheet.getSheetByName(SHEET_NAME);
sheet.clearContents();
}
function getActiveBudgetOrder() {
// There will only be one active budget order at any given time.
var budgetOrderIterator = AdsApp.budgetOrders()
.withCondition('status="ACTIVE"')
.get();
while (budgetOrderIterator.hasNext()) {
var budgetOrder = budgetOrderIterator.next();
Logger.log("Budget Order Amount " + budgetOrder.getSpendingLimit());
}
}
Assuming you want to clear the entire Sheet every time you extract the data this should work for you. You will need to set the url and shtName variables.
function getActiveBudgetOrder() {
var url = 'https://docs.google.com/spreadsheets/d/xxxxxxxxxxxxxxxxxxxxxxx/';
var shtName = 'Sheet1';
var arr = [];
var sht = SpreadsheetApp.openByUrl(url).getSheetByName(shtName);
// There will only be one active budget order at any given time.
var budgetOrderIterator = AdsApp.budgetOrders()
.withCondition('status="ACTIVE"')
.get();
while (budgetOrderIterator.hasNext()) {
var budgetOrder = budgetOrderIterator.next();
arr.push(["Budget Order Amount " + budgetOrder.getSpendingLimit()]);
}
sht.clearContents();
sht.getRange(1, 1, arr.length, arr[0].length).setValues(arr);
}
I would like to search my google sheet for a value, the value that I am looking for is an ID for customers; 'SGK - xxx' is the format of the ID (xxx is a number).
I am currently using the following code:
var data = spr.getDataRange().getValues();
for(n=0;n<data.length;++n){
if(data[n][0].toString().match(CustomerNumber)==CustomerNumber){
data[n][19] = emailCustomer
var KlantNR = data[n][0];
var Email = data[n][3];
var Stad = data[n][10];
var Taalschool = data[n][11];
var Cursus = data[n][12];
var Weken = data[n][13];
var Accommodatie1 = data[n][15];
var TypeAccommodatie = data[n][16];
var TypeKamer = data[n][17];
var VertrekDatum1 = data[n][18];
};
}
Logger.log(data)
spr.getRange(1,1,data.length,data[0].length).setValues(data);
For example: When I search on 'SGK - 4', this code returns 'SGK - 499'. How can I edit this code to return the correct value?
.match(CustomerNumber)
When I remove that line of code it works fine..
I am trying to develop a gradebook with Google Spreadhseets, but I only want to display individual grades based on user email. Is there a Google Apps Script function that looks at a logged in user's email, searches the spreadhseet for it (it would be at the top of a column) and then displays that column back to the user?
I think the best solution would probably be to create a small webapp Ui that shows only the relevant column in a html table. Depending on your programming skills it can be quite easy to setup. I could have suggested an example but you gave too few information about the type of data that are in the spreadsheet... User's data are in a single column but I suppose there is also some kind of headers or row identifiers that must be shown to understand what is shown ?
Users will have to authorize the webapp in order to let you identify them when they are logged in.
Edit : here is a simple example of a possible solution that you can start from.
I assumed mails are in row 1, descriptions in column A.
var ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheet/ccc?key=0AnqSFd3iikE3dGdQOXZpRmJnT0xjQklkdEZING5YREE#gid=0');
var sh1 = ss.getSheetByName('sheet1');
var logsheet = ss.getSheetByName('logsheet');
var data = sh1.getDataRange().getValues();
var user = Session.getEffectiveUser()
function doGet() {
var app = UiApp.createApplication();
if(!getcol(user)){
var warn = app.createTextBox().setWidth('500').setValue("Your results are not available or you don't have permission to view these data");// if user is not in the list, warning + return
app.add(warn)
return app
}
var grid = app.createGrid(data.length, 2).setBorderWidth(1).setCellPadding(2);
var text = app.createTextBox().setName('text').setId('text').setValue(user+' | idx'+getcol(user) ).setWidth('300px');
var btn = app.createButton('press here to confirm you have view these results');
var handler = app.createServerHandler('log').addCallbackElement(grid);
btn.addClickHandler(handler);
var col = getcol(user)
grid.setWidget(0,1,text).setText(0, 0, 'Results for');
grid.setStyleAttribute('textAlign','center')
for(n=1;n<data.length;++n){
grid.setText(n, 0, data[n][0])
grid.setText(n, 1, data[n][col])
}
app.add(grid).add(btn)
return app
}
function log(e){
var text = e.parameter.text
var app = UiApp.getActiveApplication();
// add user name to the log sheet + timestamp + eventually send a mail etc...
return app
}
function getcol(mail){
if(data[0].toString().indexOf(mail.toString())!=-1){
for(zz=1;zz<data[0].length;++zz){
if(data[0][zz] == mail){var colindex=zz;break}
}
return colindex
}
return false
}
EDIT2 :
To print numbers with the desired number of decimals you can use the recently implemented printf function like this :
grid.setText(n, 1, Utilities.formatString('%.2f',data[n][col]));// 2 decimals, no leading 0
to show dates like you want use formatDate()
Utilities.formatDate(date, Session.getTimeZone(), "MM-dd")
EDIT3 :
here is a version that handles the different types of data :
var ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheet/ccc?key=0AnqSFd3iikE3dGdQOXZpRmJnT0xjQklkdEZING5YREE#gid=0');
var sh1 = ss.getSheetByName('sheet1');
var logsheet = ss.getSheetByName('logsheet');
var data = sh1.getDataRange().getValues();
var user = Session.getEffectiveUser()
Logger.log(user)
function doGet() {
var app = UiApp.createApplication();
if(!getcol(user)){
var warn = app.createTextBox().setWidth('500').setValue("Your results are not available or you don't have permission to view these data");// if user is not in the list, warning + return
app.add(warn)
return app
}
var grid = app.createGrid(data.length, 2).setBorderWidth(1).setCellPadding(2).setStyleAttribute('background','#ffeeaa').setId('grid');
var text = app.createTextBox().setName('text').setId('text').setValue(user+' | idx'+getcol(user) ).setWidth('300px');
var btn = app.createButton('press here to confirm you have view these results');
var handler = app.createServerHandler('log').addCallbackElement(grid);
btn.addClickHandler(handler);
var col = getcol(user)
grid.setWidget(0,1,text).setText(0, 0, 'Results for');
grid.setStyleAttribute('textAlign','center')
for(n=1;n<data.length;++n){
grid.setText(n, 0, string(data[n][0]));
grid.setText(n, 1, string(data[n][col]));
}
grid.setStyleAttributes(n-1, 0, {'fontWeight':'bold','background':'#ffff99'})
grid.setStyleAttributes(n-1, 1, {'fontWeight':'bold','background':'#ffff99'})
app.add(grid).add(btn)
return app
}
function log(e){
var text = e.parameter.text
var app = UiApp.getActiveApplication();
app.getElementById('grid').setStyleAttribute('background','#bbffbb')
// add user name to the log sheet + timestamp + eventually send a mail etc...
return app
}
function string(value){
Logger.log(typeof(value))
if (typeof(value)=='string'){return value};// if string then don't do anything
if (typeof(value)=='number'){return Utilities.formatString('%.1f / 20',value)};// if number ther format with 1 decimal
if (typeof(value)=='object'){return Utilities.formatDate(value, Session.getTimeZone(), "MM-dd")};//object >> date in this case, format month/day
return 'error'
}
function getcol(mail){
if(data[0].toString().indexOf(mail.toString())!=-1){
for(zz=1;zz<data[0].length;++zz){
if(data[0][zz] == mail){var colindex=zz;break}
}
return colindex
}
return false
}