Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

To fetch data from Rest api to google sheet using Appscript

shankara vijayendran
Contributor
August 22, 2023

I am using Google appscript in which we have to write code to fetch only need data from Rest API 

 

function importProjectInfo() {
  var options = {
    "method": "GET",
    "headers": {
      "Content-Type": "application/json",
      "Authorization": "Basic amlyYWFwaXN1cHBvcnRAdG5xdGVjaC5jb206RXB2eXl0TFFBNjRjb0l5akdyMEEyRjMy"
    }
  };

  var response = UrlFetchApp.fetch(apiUrl, options);

  Logger.log("Response Status Code: " + response.getResponseCode());
  Logger.log("Response Content: " + response.getContentText());

  var data = JSON.parse(response.getContentText());
  var projectInfo = [];

  if (data && Array.isArray(data)) {
    projectInfo = data.map(function (project) {
      var projectId = project.id;
      var projectName = project.name;

      // Fetch roles for each project
      var rolesUrl = "https://tnq.atlassian.net/rest/api/2/project/" + projectId + "/role";
      var rolesResponse = UrlFetchApp.fetch(rolesUrl, options);
      var rolesData = JSON.parse(rolesResponse.getContentText());

      var roles = {};

      if (Array.isArray(rolesData)) {
        rolesData.forEach(function (role) {
          roles[role.name] = role.actors.map(function (actor) {
            return actor.displayName;
          });
        });
      }

      return {
        project: projectName,
        roles: roles
      };
    });
  }

  // Output project info to spreadsheet
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var headers = ["Project", "Roles"];
  sheet.getRange(1, 1, 1, headers.length).setValues([headers]);

  projectInfo.forEach(function (info, index) {
    var row = [info.project, JSON.stringify(info.roles)];
    sheet.getRange(index + 2, 1, 1, row.length).setValues([row]);
  });
}

The app script was able to fetch only project names from the API
https://xxx.atlassian.net/rest/api/2/role
but not users and user roles

Even IF account ID is fetched and project Key is fetched it will good 

0 answers

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events