Skip to content

Instantly share code, notes, and snippets.

@snipe
Last active December 12, 2023 22:41
Show Gist options
  • Save snipe/7b5c11c510af564c319ab368bb1ff4e7 to your computer and use it in GitHub Desktop.
Save snipe/7b5c11c510af564c319ab368bb1ff4e7 to your computer and use it in GitHub Desktop.
Google App Script to bulk change asset tags in Snipe-IT by asset ID

Snipe-IT offers a way to bulk regenerate asset tags all at once, but updating only certain asset tags in bulk can only be done via API, since asset tag is currently the unique identifier for assets within Snipe-IT.

This Google App Script will let you provide a list of Asset IDs and the new asset tags they should use, along with showing you the status of the request, without having to know how to write code to use the Snipe-IT API.

This script expects a CSV that has a header row (though it doesn't matter what the header names are) with the first column being the internal Snipe-IT asset ID, and the second column being the new asset tag you want to change it to.

Screenshot 2023-12-12 at 10 07 09 PM

Code:

//@OnlyCurrentDoc

function updateSelectedAssetTags() {

  // START customization
  
  var api_baseurl = 'https://your-url/api/v1';  // fully qualified url to the base api - should end in /v1, no trailing slash"
  var api_bearer_token = 'Bearer YOUR-BEARER-TOKEN';  // should start with "Bearer "
  // END customization

  var sheet = SpreadsheetApp.getActive();
  var data = sheet.getDataRange().getValues();
  let i = 1;
 
  data.forEach(function (row) {

    if (i > 1)  {

      let asset_id = Math.floor(row[0]);
      let new_asset_tag = row[1];
      let url = api_baseurl + '/hardware/' + row[0];
      
      var options = {
        method: 'patch',
        muteHttpExceptions: true,
        headers: {
          Accept: 'application/json',
          Authorization: api_bearer_token, 
          'Content-Type': 'application/json',
        },
        payload: JSON.stringify(
          {
            asset_tag: new_asset_tag,
          }),
      };

      // Make the http request and collect and parse the response
      var response = UrlFetchApp.fetch(url, options);
      var response_msg = JSON.parse(response.getContentText());

      Logger.log('Asset ID: ' + asset_id + ' - new asset tag should be ' + new_asset_tag);
      Logger.log('Payload: ' + options.headers.payload);
      Logger.log('Endpoint: ' + url);
      Logger.log('Row Array: ' +  row);
      Logger.log('HTTP Response Code: ' + response.getResponseCode());
      Logger.log('Messages: ' + response_msg.messages);

      if ((response.getResponseCode() === 200) && (response_msg.status == 'success')) {
        var status_color = '#b6d7a8';
      } else {
         var status_color = '#f4cccc';
      }

      // Put the status in the third column for each row
      sheet.getRange("C" + Math.floor(i)).setValue(response_msg.messages).setBackground(status_color);
    
    }
    i++;
    
    // This is used for very large imports so as not to exceed API throttle limits
    Utilities.sleep(1000);
    
  });
  // end foreach 
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment