This article will explain how to schedule exports via the Mobenzi API using Google Apps Scripts. An email will be sent with the link to export the data. Using reports you can specify the data to be exported - for example, export data for a specific site /area or filtered by a specific data collector. 


Add a new export

  1. Go to script.google.com on the Google account where you want to run the script. If you don’t have Apps Script setup yet, this guide should help to set it up: https://developers.google.com/apps-script/overview
  2. Add a new script function. See example below.
function requestExport() {  

  //API user login details:
  const username = "user@example.com";
  const password = "MyExamplePassword";

  //Form export settings:
  const formData = {
    "format": "CSVSeparate",
    "surveyId": "123456", 
    "emailAddress": "user@example.com", 
    "reportId": "VgkcJqdksECDcDw5VT1frg" 
  };


  const options = {
    "method": "post",
    "payload": formData,
    "headers": {
      "Authorization": `Basic ${Utilities.base64Encode(username + ":" + password)}`
    }
  };
  
  const baseUrl = "https://console.mobenzi.com/secure/api.svc";

  const response = UrlFetchApp.fetch(baseUrl + "/ExportData", options);
  Logger.log(response);
}


Be sure to replace the following with your specific details:

  • username and password: These are the login credentials for your account’s API user (or any user that has permission to perform the operations you would like to via the API). Important note: For a production implementation, these credentials should never be stored in plain text but rather in a secure environment file or property. They are only placed in code here for simplicity.
  • format: The selected export format, e.g. “CSVSeperate” (for merged CSV export) or “Excel2007” (for an Excel file export, version 2007 and newer).
  • surveyId: The integer identifier of the survey/form. This can be found in a couple of places, but one example is in the URL of the Form Overview page on the Console as the parameter “id”. E.g in the url console.mobenzi.com/mod/survey/view/?study_id=3437&id=262321 the surveyId is “262321”.
  • emailAddress: The email to which the export should be sent. Note that the user that receives the email with the export will need to be able to log in to the account.
  • reportId: The unique identifier (UID) of the published report that should be exported. This is available on the report settings tab as “Report Id”. This parameter is optional - if none is provided all survey submissions will be exported.


3. Test your script by using the “Debug” command in the top panel.

4. Click on “Run to execute the script. 


Notes on exporting reports:

  • Reports must be saved as a "Public Published Report" in order to export it via the API.
  • If you do not wish to send a specific report, you can simply remove the line "reportId": from your script.
  • Read more about setting up reports here.


Schedule your export


Once you are happy that it runs as expected, you can schedule it to run on a time-driven trigger (e.g. once a month). Navigate to “Triggers” in the left-hand panel and click “Create a new trigger”. Fill in the form. Here is an example of a script scheduled to run once a month:


image.png




Your script should now run based on the schedule you have set up. You can check the execution of the triggers on the “Executions” tab in the left-hand panel:


image.png