2 Replies Latest reply on Aug 6, 2018 3:11 AM by ashish bansode

    Tableau Java script API download data from getunderlinedataAsync

    ashish bansode

      Hello Guru's,

       

      I am new to this site as mostly was into development of VBA and stuffs, hope will get good response here too.

      My main objective is to get data from tableau and save it in SQL server as tableau at my org store very less of data and we need at least 6 months data for analysis.

      i am trying to get tableau data from the tableau server through a getunderlinedataAsync option but failing to do so due to restriction from server side which return 403 forbidden code.

      i tried with the report which has permission to extract as cross tab and underlying data and was successfully able to get the data.

      My question here is can i be able to get the data through the Async option even if its blocked from the server side if yes any link / Suggestions will do fine.

      if no can i create my own report through Jquery and attach a data source to tableau workbook and then get the data from there as being a power user i will have all the download option.

      here's my code which i am using currently, also let me know if any mistakes or suggestions to improve it. Many thanks

       //To prevent a bug in IE where the global use of the 'viz' variable
      //  interferes with the div id 'viz' (in the html), the following line is needed:
      
      
      var viz, workbook, sheet,worksheet;
      
      
      window.onload= function() {
      var vizDiv = document.getElementById('viz');
      var vizURL = "http://Server/RepeatInteractionReport_2/InteractionSummary-Type?:iid=1&:embed=y&:showVizHome=y&:jsdebug=y&:apiID=host0#navType=0&navSrc=Parse&3";
      var options = {
      width: '1200px',
      height: '540px',
      hideToolbar: false,
      hideTabs: false
      };
      viz = new tableauSoftware.Viz(vizDiv, vizURL, options);
      };
      
      
      
      
      
      
      function getVizData() {
        options = {
      
      
          maxRows: 0, // Max rows to return. Use 0 to return all rows
          ignoreAliases: false,
          ignoreSelection: true,
          includeAllColumns: false
        };
      
      
        sheet = viz.getWorkbook().getActiveSheet();
      
      
      
      
      if (sheet.getSheetType() === 'worksheet') {
              sheet.getUnderlyingDataAsync(options).then(function (t) {
                  buildMenu(t);
              });
              
          //if active sheet is a dashboard get data from a specified sheet
          } else {
              worksheetArray = viz.getWorkbook().getActiveSheet().getWorksheets();
              for (var i = 0; i < worksheetArray.length; i++) {
                  worksheet = worksheetArray[i];
                  sheetName = worksheet.getName();
                  if (sheetName == 'Type Working') {
                      worksheetArray[i].getSummaryDataAsync(options).then(function (t) {
                          buildMenu(t);
                      });
                  }
              }
          }
      }
      
      
      
      
      //restructure the data and build something with it
      function buildMenu(table) {
      
      
        //the data returned from the tableau API
        var columns = table.getColumns();
        var data = table.getData();
      
      
        //convert to field:values convention
        function reduceToObjects(cols, data) {
          var fieldNameMap = $.map(cols, function(col) {
            return col.$impl.$fieldName;
          });
          var dataToReturn = $.map(data, function(d) {
            return d.reduce(function(memo, value, idx) {
              memo[fieldNameMap[idx]] = value.value;
              return memo;
            }, {});
          });
          return dataToReturn;
        }
      
      
        var niceData = reduceToObjects(columns, data);
      
      
        //take the niceData and send it to a csv named TableauDataExport
        alasql("SELECT * INTO CSV('TableauDataExport.csv',{headers:true}) FROM ?", [niceData]);
      
      
      }