How do I print the output of google sheets script back to google sheet? - api

I am trying to use an api for verifying phone numbers and emails for the database I want to create in google sheets. I coded the following on app scripts of sheet.
function phone(phno) {
var response = UrlFetchApp.fetch("https://neutrinoapi.net/phone-validate?user-id=USERNAME_HERE&api-key=API_KEY_HERE&number="+phno+"&country-code=IN")
// Parse the JSON reply
var json = response.getContentText();
var data = JSON.parse(json);
Logger.log(data["valid"]);
Logger.log(data["prefix-network"]);
Logger.log(data["type"]
}
The json returns a bunch of things, valid = true\false {If the number is valid}; prefix-network = service_provider; type = mobile\landline etc.
The idea is that if I call = phone(D2) in cell E2 for example, where D2 stores a valid phone number, I want the validity to be shown in E2, which is a boolean of true\false for if the phone number is valid or not. I would also love to show the next 2 columns with prefix-network and type. But they aren't necessarily essential, just desirable.
The API works fine, and I can see the output in the execution logs in App Scripts, and it's correct with a few test data I tried. However, I cannot find any way to display the result back in the sheet in cell E2. The code has to be generic such that if I drag the code for the cell, it should return the output to the subsequent cells.
I hope the question is clear and the code is sufficient to explain it.
Looking forward to the replies.
Thanks in advance for the help!!

Something like this:
function phone(phno) {
var response = UrlFetchApp.fetch("https://neutrinoapi.net/phone-validate?user-id=USERNAME_HERE&api-key=API_KEY_HERE&number="+phno+"&country-code=IN")
// Parse the JSON reply
var json = response.getContentText();
var data = JSON.parse(json);
Logger.log(data["valid"]);
Logger.log(data["prefix-network"]);
Logger.log(data["type"]
const sh = SpreadsheetApp.getActiveSheet();
sh.getRange(1,1,1,3).setValues([[data["valid"],data["prefix-network"],data["type"]]]);
}

Related

How to call api in powerbi to get data?

I am a bit beginner of powerbi, I want to call api from power bi to get some data.
I tried to write this code but always return this error once I clicked invoke button
Here is the code that I used:
let
GetWorkItemIds = () =>
let
Source = Json.Document(Web.Contents("https://dev.azure.com/XXX/XXX/_apis/wit/workitems/13?api-version=7.0")),
workItems = Source[workItems],
#"Converted to Table" = Table.FromList(workItems, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
#"Expand Ids" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"id"}, {"Work Item Id"})
in
#"Expand Ids"
in
GetWorkItemIds
Anyone can give idea please. Thanks a lot
2nd try:
() =>
let body = [username = "", password = "xxxxxx"],
Data = Json.Document(Web.Contents("https://dev.azure.com/X/X/_apis/wit/workitems/13")),
result = Record.Field(Data[result] {
0
}, "token")
in
result
this is my second try, it also return the same error as the picture above.
Your JSON isn't correct. Try this and see what comes back in the preview window.
let
Source = Web.Contents("https://dev.azure.com/XXX/XXX/_apis/wit/workitems/13?api-version=7.0")
in
Source
Based on the documentation on the API endpoint response, it looks to me like you are treating the response erroneously. The endpoint does not return a list of work items, but a JSON representing one singular work item based on its ID - in your case ID = 13.
As you can see in the linked documentation, the response does not contain anything called workItems, which I suspect is causing your error.
However, I think your error stems from lack of authentication.

Google Sheets API spreadsheets.values.get returning wrong date value

I'm accessing Google spreadsheet data from node.js as follows:
const sheets = google.sheets({version: 'v4', auth});
const res = await sheets.spreadsheets.values.get({
spreadsheetId,
range
});
const rows = res.data.values;
For reference, I'm using spreadsheets.values.get.
There is one cell with the value 9/24/2019. It's formatted as a date and is definitely not stored as a string. However, the value for that cell from the API call is "9/24/3019".
There are other cells with the same value that are correct. I checked the document version history, and this cell has never had any other value.
Anyone have any ideas what might be causing this and/or how to fix it?

Firestore document with umlaut, two different "ö"

My problem is, when I try to set up a new document in my firestore with a name including umlaut "ö" it writes it in a worse way. Can you compare both documents and tell me what the difference between these two "ö" are? In the first picture the "ö" is bigger than in the second picture. Because of that my further functions - for example search function which is looking for the document name - is not working for document names with umlaut. I can't figure out the answer of my problem. I hope you guys can show me the right way to handle this. I don't want to replace the umlauts.
Should I decode my variable which I pass as the document name in my setup function?
First image:
Second image:
Update:
I will explain a little bit more about my goal. I have an index.html upload form for multi-image upload to Firebase storage and writing the imageurl and other information to the |irestore. When I upload my image folder, I retrieve the path of the imagedata from my system and make a split to have only the foldername. I use this name as the document name for my firestore (it is working for folders without an umlaut in the name). But when I write the same name for creating a document through the firebase console or replace it with a variable text = "my string for foldername" it is not matching. I would say the retrieved foldername has a different coding for example for the letter "ö".
var relpath = files[i].webkitRelativePath;
folder = relpath.split("/");
var foldername= "";
//foldername = unescape(encodeURIComponent(folder[0]));
foldername = folder[0];
var storage = firebase.storage().ref().child('kitaDE/duesseldorf/'+foldername+'/'+files[i].name);
//upload file
var upload = storage.put(files[i]); //webkitRelatviPath hinzugefügt
//update progress bar
upload.on(
"state_changed",
function progress(snapshot) {
var percentage =
(snapshot.bytesTransferred / snapshot.totalBytes) * 100;
document.getElementById("progress").value = percentage;
},
function error() {
alert("error uploading file");
},
function complete() {
document.getElementById(
"uploading"
).innerHTML += `${files[i].name} uploaded <br />`;
},
);
db.collection("kitaDE").doc(foldername).set({
image: [],
id: "",
active: true,
title: "",
street: "",
zipcode: "",
location: "",
})
Update 2
I copy & paste the foldername and my direct entry for the name over the firebase console.
Foldername copied:
Am Köhnen
Entered name in firbase console through my keyboard:
Am Köhnen
It looks for me the same. I run my javascript code and give out the following part on the console log.
var relpath = files[i].webkitRelativePath;
folder = relpath.split("/");
var foldername= "";
foldername = folder[0];
var foldername2 = "Am Köhnen";
var foldername3 = decodeURIComponent(escape(foldername2))
My result is the following screenshot.
Console.log Output
You can see that first name seems right, but first and the third output names are not matching. It seems like they are the same but they not, i refer here to my both picture at the begin of my post here. Firestore handle the names different.
To get a hex dump, I ran this command in the parent directory of the problematic one:
bash$ printf '%s\n' Am\K*hnen | xxd
00000000: 416d 204b 6fcc 8868 6e65 6e0a Am Ko..hnen.
There are multiple sequences that result in an ö character being displayed. One of them uses a single Unicode codepoint to represent the character (U+00F6), but the other actually uses a separate codepoint for the o and then another one for the umlaut (U+006F U+0308).
Also see:
The wikipedia page on combining characters
The wikipedia list of unicode characters
My first idea is that the two titles in your documents are written with different Unicode sequences.
I thought that Firestore would equate these two ways of writing, but I can't find anything in the documentation about that now. If it doesn't, then that would explain why a query that matches one of the codepoint combinations for ö doesn't match the other combination.

Google Apps Script copying data to wrong sheet

I am using this code which is supposed to copy data from "REQUEST" sheet to "MASTER" sheet but actually the code copies data to "MASTER" sheet as well as some other sheet also in the file.
function onFormSubit () {
var app = SpreadsheetApp;
var ss = app.getActiveSpreadsheet();
var shtForm = ss.getSheetByName("REQUEST");
var shtAgrt = ss.getSheetByName("MASTER");
var rcrdCount = shtAgrt.getRange(1, 25).getValue();
var chkNum = shtForm.getRange(Math.max(shtForm.getLastRow(),2),1).getValue();
shtAgrt.getRange(rcrdCount+1, 2).setValue(chkNum);
}
I don't want the code to copy data to any other sheet.
I don't understand what happened but error is gone away now. Perhaps it went away after deleting all form submit data or after refreshing google sheet.

Auto Update specified tab in Google spreadsheet from Web via script

I am currently trying to pull in data from a website into my Google Sheet, however, it will automatically pull the data on whatever tab I am on when the sheet updates.
The script I currently have is below. I have 5 tabs in the workbook and the tab I need updated is labeled 'Update' and is the 3rd tab in the workbook.
The data currently pulls in correctly, however, it will not only update the tab I need updated.
function getData() {
var queryString = Math.random();
var cellFunction = '=IMPORTHTML("http://www.golfchannel.com/tours/pga-tour/2017/us-open/?' + queryString + '","table",2)';
SpreadsheetApp.getActiveSheet().getRange('A1').setValue(cellFunction);
}
Change your code line to this:
SpreadsheetApp.getSheetByName('Update').getRange('A1').setValue(cellFunction);
You can set the sheet by calling the method getSheetByName().
Returns a sheet with the given name. If multiple sheets have the same name, the leftmost one is returned. Returns null if there is no sheet with the given name.
Here is a sample snippet:
function myFunction() {
var sss = SpreadsheetApp.openById('FILEID'); // sss = source spreadsheet
var ss1 = sss.getSheetByName('Sheet1');
var ss2 = sss.getSheetByName('Sheet2');
var source_range = ss1.getRange("A1:G10");
var target_range = ss2.getRange("A1:G1");
source_range.copyTo(target_range);
}
Hope this helps.