Script example for multiple values

4,145 views
Skip to first unread message

Joshua Gilman

unread,
Sep 26, 2014, 3:50:44 PM9/26/14
to flore...@googlegroups.com
Greetings,

I noticed the default Google Apps Script generated from within Ultradox only uses single values for the inputs and outputs. I couldn't find any more examples on the help portion of the website that demonstrated the proper syntax and usage of multiple values. Could someone please provide an example for multiple value inputs and outputs? Cheers.

Daniel Florey

unread,
Sep 26, 2014, 5:33:33 PM9/26/14
to floreysoft GmbH
Hi,
I'll provide another example to help users to get started... You'll basically have to return an array of objects.

Hope this helps,
Daniel 

On Fri, Sep 26, 2014 at 9:50 PM, Joshua Gilman <jmgi...@google.com> wrote:
Greetings,

I noticed the default Google Apps Script generated from within Ultradox only uses single values for the inputs and outputs. I couldn't find any more examples on the help portion of the website that demonstrated the proper syntax and usage of multiple values. Could someone please provide an example for multiple value inputs and outputs? Cheers.

--
You received this message because you are subscribed to the Google Groups "floreysoft" group.
To unsubscribe from this group and stop receiving emails from it, send an email to floreysoft+...@googlegroups.com.
To post to this group, send email to flore...@googlegroups.com.
Visit this group at http://groups.google.com/group/floreysoft.
For more options, visit https://groups.google.com/d/optout.

Joshua Gilman

unread,
Sep 27, 2014, 11:29:33 AM9/27/14
to flore...@googlegroups.com
Where can I find these? Also, how do I pass off static arrays to a script that takes an array as an input? I need to provide a static list of strings to my script each time it executes.

--
You received this message because you are subscribed to a topic in the Google Groups "floreysoft" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/floreysoft/i38QGBbrmUY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to floreysoft+...@googlegroups.com.

To post to this group, send email to flore...@googlegroups.com.
Visit this group at http://groups.google.com/group/floreysoft.
For more options, visit https://groups.google.com/d/optout.



--
Joshua Gilman
DC Ops (DLS)

Daniel Florey

unread,
Sep 27, 2014, 11:33:50 AM9/27/14
to floreysoft GmbH
This is a copy of my "Get directions" script. Check out the transformedSteps array as an example.
The model can contain any objects so you can pass an array into the script by simply binding the array to a variable.

---

function execute(model) {
  var mode = Maps.DirectionFinder.Mode.DRIVING;
  var travelMode = model.directionsMode;
  var depart = new Date();
  if ( model.directionsDeparture ) {
    var dateString = model.directionsDeparture.replace(/-/g, '/');
    depart = new Date(dateString);
  }
  if ( travelMode ) {
    mode = travelMode.toLowerCase();
  }
  var directions = Maps.newDirectionFinder()
     .setOrigin(model.directionsOrigin)
     .setDestination(model.directionsDestination)
     .setDepart(depart)
     .setMode(mode)
     .getDirections();
  var transformedSteps = [];
  var route = directions.routes[0];
  if ( route ) {
    if ( route.legs ) {
      var leg = route.legs[0];
      if ( leg ) {
        var duration = leg.duration.text;
        var distance = leg.distance.text;
        var steps = leg.steps;
        if ( steps ) {
          for ( var i = 0; i < steps.length; i++ ) {
            var step = steps[i];
            var latlng = model.directionsDestination;
            if ( i < steps.length ) {
              latlng = dec2StrLat(step.end_location.lat)+","+dec2StrLng(step.end_location.lng);
            }
            transformedSteps.push({ instructions : step.html_instructions, distance : step.distance.text, duration : step.duration.text, location : latlng, travelMode : step.travel_mode });
          }
        }
      }
    }
  }
  return { directionsDistance : distance, directionsDuration : duration, directionsRoute : route, directionsSteps : transformedSteps };
}

function dec2StrLat(decLatitude) {
  var intDegree;
  var decMinute;
  var strLatitude = "N";
  if (decLatitude < 0) {
    strLatitude = "S";
    decLatitude = decLatitude * -1;
  }
  intDegree = Math.floor(decLatitude);
  decMinute = (decLatitude - intDegree) * 60;
  decMinute = Math.round(decMinute*1000)/1000;
  strLatitude = strLatitude + " " + String(intDegree) +"° " + String(decMinute) +"'";
  return strLatitude
}

function dec2StrLng(decLongitude) {
  var intDegree
  var decMinute
  strLongitude = "E";
  if (decLongitude < 0) {
    strLongitude = "W";
    decLongitude = decLongitude * -1;
  }
  intDegree = Math.floor(decLongitude);
  decMinute = (decLongitude - intDegree) * 60;
  decMinute = Math.round(decMinute*1000)/1000;
  strLongitude = strLongitude + " " + String(intDegree) +"° " + String(decMinute) +"'";
  return strLongitude
}

function getInputParameters() {
  var SINGLE_VALUE = 0;
  var LIST = 1;
  var CONDITION = 2;

  return [
          { name : 'directionsOrigin',
            description : 'The origin of the directions',
            type : SINGLE_VALUE
          },
          { name : 'directionsDestination',
            description : 'The destination of the directions',
            type : SINGLE_VALUE
          },
          { name : 'directionsMode',
            description : 'The travel mode can be driving, walking, bicycling or transit',
            type : SINGLE_VALUE
          },
          { name : 'directionsDeparture',
            description : 'The departure time, required when using transit mode',
            type : SINGLE_VALUE
          }
         ];
}

function getOutputParameters() {
  var SINGLE_VALUE = 0;
  var LIST = 1;
  var CONDITION = 2;

  return [
          { name : 'directionsDistance',
            description : 'The total distance',
            type : SINGLE_VALUE 
          },
          { name : 'directionsDuration',
            description : 'The total duration',
            type : SINGLE_VALUE 
          },
          { name : 'directionsRoute',
            description : 'The complete route object - for experts!',
            type : SINGLE_VALUE 
          },
          { name : 'directionsSteps.instructions',
            description : 'The driving instructions for each step of the route',
            type : LIST 
          },
          { name : 'directionsSteps.distance',
            description : 'The distance for each step of the route',
            type : LIST 
          },
          { name : 'directionsSteps.duration',
            description : 'The duration for each step of the route',
            type : LIST 
          },
          { name : 'directionsSteps.location',
            description : 'The location for each step to be rendered as a map',
            type : LIST 
          },
          { name : 'directionsSteps.travelMode',
            description : 'The travel mode for each step (e.g. WALKING,DRIVING,TRANSIT)',
            type : LIST 
          }          
          
         ];
}

function doGet(request) {
  try {
    var command = request.parameters.cmd;
    var result = { error : 'Invalid script invokation!' };
    if (typeof command != 'undefined' ) {
      if ( command == 'ip' ) {
        result = getInputParameters();
      } else if ( command == 'op' ) {
        result = getOutputParameters();
      } else {
        return HtmlService.createHtmlOutput('<html><body style="height:500px;background-image:url(http://www.ultradox.com/ultradoxBg.png);background-repeat: no-repeat;background-position: right top;"><table cellspacing="25px" width="450px"><tr><td rowspan="2"><img src="http://www.ultradox.com/ultradoxOk.png"></td><td style="font:22px Ubuntu">Access granted</td></tr><tr valign="top"><td style="color:#999;font:16px Ubuntu">You can now close this window and reload the script.</td></tr></table></body></html>');
      }
    }
    var json = JSON.stringify(result);
    return ContentService.createTextOutput(json).setMimeType(ContentService.MimeType.JSON);
  } catch ( err ) {
    var msg = err;
    if ( typeof err == Error ) {
      msg = err.message;
    }
    return ContentService.createTextOutput("{ __error : '"+msg+"' }").setMimeType(ContentService.MimeType.JSON);
  }
}


function doPost(request) {
  try {
    var json = request.postData.getDataAsString();
    var model = JSON.parse(json);
    var result = execute(model);
    var json = JSON.stringify(result);
    return ContentService.createTextOutput(json).setMimeType(ContentService.MimeType.JSON);
  } catch ( err ) {
    var msg = err;
    if ( typeof err == Error ) {
      msg = err.message;
    }
    return ContentService.createTextOutput("{ __error : '"+msg+"' }").setMimeType(ContentService.MimeType.JSON);
  }
}

Joshua Gilman

unread,
Sep 30, 2014, 3:35:31 AM9/30/14
to flore...@googlegroups.com
Thank you for the example, that clears most of it up. However, the website gives an example to use a Ultradox form to pass static variables to a script. At this point I don't seen an option for a form to include a static list to pass as a variable. Is there somewhere else I can create a static array to pass off?

Bruce Gilmore

unread,
Jan 21, 2016, 8:04:41 PM1/21/16
to floreysoft
What does the "CONDITION" type provide?  Can you provide an example Object?

function getOutputParameters() {
  var SINGLE_VALUE = 0;
  var LIST = 1;
  var CONDITION = 2;

Thanks BG

Daniel Florey

unread,
Jan 22, 2016, 3:05:20 AM1/22/16
to floreysoft GmbH
Hi,
I'm working on improving the script definition. "Condition" will become "Boolean" in the new format.
I'll attach an example for the new syntax, sorry no documentation yet...


// Description for method to retrieve user
function getUserDescription() {
  return {
    input : [
      { name : 'id',
        prompt : 'Please enter value for id:',
        placeholder : 'Unique identifier of the user. Unique identifier of the user.',
        description : 'Unique identifier of the user. Unique identifier of the user.',
        format : 'NUMBER',
        list : false,
        required : true
      }
    ],
    output : [
      { name : 'responseCode',
        description : 'Contains the response code of the API call',
        format : 'NUMBER',
        list : false,
        required : false
      },
      { name : 'id',
        description : 'Unique identifier of the user. Unique identifier of the user.',
        format : 'NUMBER',
        list : false,
        required : false
      },
      { name : 'name',
        description : 'Full name of the user. Full name of the user.',
        format : 'STRING',
        list : false,
        required : false
      },
      { name : 'email',
        description : 'Email address of the user. Email address of the user.',
        format : 'STRING',
        list : false,
        required : false
      },
      { name : 'status',
        description : 'Status of the user’s account. Usually active. Status of the user’s account. Usually active. Possible values: active inactive',
        format : 'STRING',
        list : false,
        required : false
      },
      { name : 'role',
        description : 'Role of the user. Role of the user. Possible values: user admin',
        format : 'STRING',
        list : false,
        required : false
      },
      { name : 'confirmed',
        description : 'Indicates whether the user’s account has been confirmed or not. Indicates whether the user’s account has been confirmed or not.',
        format : 'BOOLEAN',
        list : false,
        required : false
      },
      { name : 'created_at',
        description : 'Date and time of creation in UTC (ISO8601 format). Date and time of creation in UTC (ISO8601 format).',
        format : 'DATE_TIME',
        list : false,
        required : false
      },
      { name : 'updated_at',
        description : 'Date and time of the last update in UTC (ISO8601 format). Date and time of the last update in UTC (ISO8601 format).',
        format : 'DATE_TIME',
        list : false,
        required : false
      }
    ],
    modelRequired : false,
    switchAccountSupported : true,
    inputPrefix : 'user',
    outputPrefix : 'user'
  }
}


--
You received this message because you are subscribed to the Google Groups "floreysoft" group.
To unsubscribe from this group and stop receiving emails from it, send an email to floreysoft+...@googlegroups.com.
To post to this group, send email to flore...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages