I’m trying to use a Google sheet with device Serial numbers in Col A and populate Col B with the device record ID held in our MDM using Google Apps Script and Jamf API.
I’ve got the script getting the ID based of the device Serial in the sheet but can’t figure out how to get it to populate in column B next to the serial number. At the moment it appends the rows below the serial list in Column B (screenshot attached)…feels like I’ve read a 100 websites now and not getting anywhere…any ideas?
function JamfHttpPutRequest() {
var rows = SpreadsheetApp.openById("MYSHEETID").getSheetByName("Sheet1").getDataRange().getValues(),
range,
values_array;
rows.forEach(function(row, index){
var serialnumber = rows[index][0];
var url = "https://MYDOMAIN.jamfcloud.com/JSSResource/computers/serialnumber/" + serialnumber + "";
var response = UrlFetchApp.fetch(url, {
"method": "GET",
"headers": {
"Authorization": "Basic MYAUTH",
"Content-Type": "application/xml"
},
"muteHttpExceptions": true,
"followRedirects": true,
"validateHttpsCertificates": true,
"contentType": "application/xml",
}).getContentText();
var document = XmlService.parse(response);
var entries = document.getRootElement().getChild('general').getChild('id').getValue();
SpreadsheetApp.getActiveSheet().appendRow([null, (entries)]);
// Log items
Logger.log(serialnumber);
Logger.log(entries);
})
}
The above does this
Trying to get it to do this which I copied and pasted in the correct place
Description
.appendRow() adds a new row after the least row containing data. What you want is getRange() instead as shown below.
Code.gs
function JamfHttpPutRequest() {
var rows = SpreadsheetApp.openById("MYSHEETID").getSheetByName("Sheet1").getDataRange().getValues(),
range,
values_array;
var entries = [];
rows.forEach(
function(row, index){
var serialnumber = row[0];
var url = "https://MYDOMAIN.jamfcloud.com/JSSResource/computers/serialnumber/" + serialnumber + "";
var response = UrlFetchApp.fetch(url, {
"method": "GET",
"headers": {
"Authorization": "Basic MYAUTH",
"Content-Type": "application/xml"
},
"muteHttpExceptions": true,
"followRedirects": true,
"validateHttpsCertificates": true,
"contentType": "application/xml",
}
).getContentText();
var document = XmlService.parse(response);
var entry = document.getRootElement().getChild('general').getChild('id').getValue();
// assuming entry is a single value
entries.push([entry])
// Log items
Logger.log(serialnumber);
Logger.log(entries);
}
);
// put values in row 1 column B
SpreadsheetApp.getActiveSheet().getRange(1,2,entries.length,1).setValues(entries);
}