I'm trying to use data in a Google Sheet to create API calls. I have a Google Sheet with two columns of data. Computer serial numbers in col 1 and their corresponding asset tag number in col 2.
I'm trying to read both these values to form a URL API call.
I've managed to get this to work with specific cells but I don't know how to make this loop through the whole sheet row by row.
The script gets the sheet by ID and name (Sheet1). Selects data in A2 and B2 and these as the serial and the assettag Then we build the API call adding in the serial and asset tag.
function HttpPutRequest() {
var sheet = SpreadsheetApp.openById("xxxxxxxxxxxxxxxxxxxx").getSheetByName("Sheet1"),
range,
values_array;
serial = sheet.getRange('A2:A2');
serialnumber = serial.getValues();
asset = sheet.getRange('B2:B2');
assettag = asset.getValues();
const url = "https://MyURLAPICall/serialnumber/" + serialnumber + "";
const response = UrlFetchApp.fetch(url, {
"method": "PUT",
"headers": {
"Authorization": "Basic",
"Content-Type": "application/xml"
},
"contentType": "application/xml",
"payload": "<computer><general><asset_tag>" + assettag + "</asset_tag></general></computer>"
});
Logger.log(serialnumber);
Logger.log(assettag);
Logger.log(url);
Logger.log("Response code is %s", response.getResponseCode());
Logger.log(response.getContentText());
}
This works fine for a single row of data in row 2 col 1 and col 2 but how could it then move down to row 3, col 1 & col 2, then row 4 etc etc.