Skip to content

Instantly share code, notes, and snippets.

@JensWalter
Created January 9, 2020 17:25
Show Gist options
  • Save JensWalter/673751f9941d42477f24d53dd5dad31b to your computer and use it in GitHub Desktop.
Save JensWalter/673751f9941d42477f24d53dd5dad31b to your computer and use it in GitHub Desktop.
Calling SAP BAPIs from Google Sheets
var endpoint ="https://callable-xxxxxxxxx-uc.a.run.app";
var user = "user1";
var password = "secret1";
function invokeRFC(rfcName,payload) {
var options = {"method":"POST", "payload":payload, headers: {}};
//check authentication data
if(user.length>0){
var auth = "Basic " + Utilities.base64Encode(user + ":" + password);
options.headers['Authorization']=auth;
}
//call API
var jsondata = UrlFetchApp.fetch(endpoint+"/rfc/"+rfcName, options);
var obj = JSON.parse(jsondata.getContentText());
var rows=[];
var header=[];
//get list structure
var names = Object.keys(obj);
var listName ="";
for(var col in names){
if(names[col]!="RETURN"){
listName=names[col];
}
}
//get rows
var list = obj[listName];
for(var idx=0;idx<list.length;idx++){
var entry = list[idx];
var row=[];
var cols=Object.keys(entry);
for(var col in cols){
header[col]=cols[col];
row.push(entry[cols[col]]);
}
rows.push(row);
}
rows.unshift(header);
return rows;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment