Skip to content

Instantly share code, notes, and snippets.

@acarrillo
Created June 13, 2013 09:44

Revisions

  1. acarrillo created this gist Jun 13, 2013.
    57 changes: 57 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,57 @@
    var API_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
    var mc_base_url = 'http://us7.api.mailchimp.com/1.3/?method=listSubscribe';
    var mc_list_id = 'xxxxxxxxxx';
    var mc_double_optin = false;


    /**
    * Uses the MailChimp API to add a subscriber to a list.
    */
    function sendToMailChimp_(fn, ln, em, yr){
    var payload = {
    "method": "listSubscribe",
    "apikey": API_KEY,
    "id": mc_list_id,
    "merge_vars[FNAME]":fn,
    "merge_vars[LNAME]":ln,
    "merge_vars[YEAR]":yr,
    "email_address": em,
    "double_optin": mc_double_optin,
    "update_existing": false
    };
    var options = {
    "method": "post",
    "payload": payload
    };

    var response = UrlFetchApp.fetch(mc_base_url,options);
    Logger.log(response)
    }

    /**
    * Trigger function. Based on Google Script tutorial.
    * @param {Object} e The event parameter for form submission to a spreadsheet;
    * see https://developers.google.com/apps-script/understanding_events
    */
    function onFormSubmit(e) {

    var fname = e.namedValues['Name'][0].split(" ")[0];
    var lname = e.namedValues['Name'][0].split(" ")[1];
    var email = e.namedValues['Email'][0];
    var year = e.namedValues['Stuyvesant Graduation Year'][0];

    sendToMailChimp_(fname,lname,email,year);

    }

    /**
    * Main function. Creates onFormSubmit trigger.
    */
    function myFunction(){
    // Was separated line by line for debugging purposes.
    var sheet = SpreadsheetApp.getActive();
    var a = ScriptApp.newTrigger("onFormSubmit");
    var b = a.forSpreadsheet(sheet);
    var c = b.onFormSubmit();
    var d = c.create();
    }