5 Replies Latest reply on Sep 16, 2013 12:58 PM by Russell Christopher

    How to use: getSelectedMarksAsync() ?

    Gordon Young

      Hi everyone. I'm new to the Tableau API and just trying some basic functions.

       

      I'm working with the sample code that has been provided and look for some help on how the getSelectedMarksAsync() function works.

       

      Here's my attempt at showing the pairs related to a selected mark.  What am I doing wrong?

       

       

      function alertFunc() {

       

        sheet = workbook.getActiveSheet();

        marksArray = sheet.getSelectedMarksAsync();

        pairsArray = marksArray[0].getPairs();

       

        for(var i = 0; i < pairsArray.length; i++) {

             alert("Field: " + pairsArray[i].fieldName + " Value: " + pairsArray[i].Value);

        }

      }

        • 1. Re: How to use: getSelectedMarksAsync() ?
          Russell Christopher

          Hey Gordon -

           

          You need to grab the pairs that come back in the promise associated with getSelectedMarksAsync().

           

          Example:

           

          // Helper functions to deal with promise callback for getSelectedMarksAsync()

           

          function onSuccess(result) {

           

            var selectedMarks = result;

            if (selectedMarks.length == 0)

            alert("selectedMarks: empty list");

            else {

            var alertOutput="";

             $.each(selectedMarks, function (i, mark) {

             alertOutput = alertOutput + "selectedMarks:\n";

            $.each(mark.getPairs(), function (j, pair) {

            alertOutput = alertOutput + "  " + (pair.fieldName) + ": " + pair.value;

            });

            alertOutput = alertOutput + "\n";

             });

            }

            alert(alertOutput);

          }

           

           

          function onError(message) {

            alertOrConsole("Error: '" + message + "'");

          }

           

           

          function getSelectedMarksAsync() {

           

            mainWorkbook = mainViz.getWorkbook();

            // Profit v Sales

            var newSheet = mainWorkbook.getActiveSheet();

            // See above for promise defintion of onSuccess, onError

            newSheet.getSelectedMarksAsync().then(onSuccess, onError);

          }

          • 2. Re: How to use: getSelectedMarksAsync() ?
            Michel Roberge

            Hi Russell,

             

            I was looking for something similar, but I still can't figure out exactly how to do what I want to. Let's say I have a dashboard, and I want to get all selected marks of all sheets. How do I do that? If there's 3 sheets in the dashboards with a couple of selected marks, and I want a function that will get all these values, how do I do it?

             

            Thanks!

             

            Michel

            • 3. Re: How to use: getSelectedMarksAsync() ?
              Russell Christopher

              Hey Michel –

               

              The API is built to deal with sheets from multiple (different) workbooks all being displayed at the same time in a portal. As a result, there is no way to ask “give me all the marks selected across all sheets that currently are being displayed”.

               

              Instead, you need to refer to each workbook & sheet individually (like in the tutorial), ask it what marks are selected, then move on to the next one. Make sense?

              • 4. Re: Re: How to use: getSelectedMarksAsync() ?
                Michel Roberge

                Sure - to some extent.

                 

                So, given one workbook. I can iterate through each worksheet, that's fine. So, let's say I have this code:

                 

                function getSelectedMarks()
                {
                   var selectedData = [];
                   if ( activeSheet.getSheetType() == tableauSoftware.SheetType.WORKSHEET ){
                      selectedData = getSheetSelectedMarks(activeSheet);
                   }
                   else{
                      var worksheets = activeSheet.getWorksheets();
                      for ( var i=0;i<worksheets.length;i++){
                         var aTempData = getSheetSelectedMarks(worksheets[i]);
                         if ( aTempData !== null ){            
                            for ( var j=0;j<aTempData.length;j++){
                               selectedData.push(aTempData[j]);
                            }
                         }
                      }
                   }
                   return selectedData;
                }
                

                 

                Because it is async, I'm not sure what the code should be for the getSheetSelectedMarks function. I tried this:

                 

                function getSheetSelectedMarks(sheet)
                {      
                   sheet.getSelectedMarksAsync().then( function(marks){
                      var name = sheet.getName();
                      var aData = [];
                      for (var markIndex = 0; markIndex < marks.length; markIndex++) {
                         var pairs = marks[markIndex].getPairs();
                         for (var pairIndex = 0; pairIndex < pairs.length; pairIndex++) {
                            var pair = pairs[pairIndex];
                            aData.push([name, markIndex, pair.fieldName, pair.value ] );
                         }
                      }
                      return aData;
                   });
                }
                

                 

                But it's not working.

                 

                Unless I misunderstand your comment, this should somehow work, no?

                 

                Thanks!

                 

                Michel

                • 5. Re: How to use: getSelectedMarksAsync() ?
                  Russell Christopher

                  You’ll want to chain these with promises:

                   

                  (See “Chaining Promises”)

                   

                  http://onlinehelp.tableausoftware.com/v8.0/server/en-us/js_api_concepts.htm

                   

                  …with the successful completion of a call against getSelectedMarksAsync() against one sheet initiating the execution of another call to do the same thing against a different sheet.