Showing posts with label JavaScript and AJAX. Show all posts
Showing posts with label JavaScript and AJAX. Show all posts

Saturday, February 23, 2013

Javascript functions in Google Spreadsheets

Hello,

Currently, all of my form submissions from my website are creating a copy of the information submitted and posting in on spreadsheets in Google Docs as well as forwarding the information to my specified email. When I open the form submissions that were received in my email, the from and/or reply to address contains my gmail email. I would prefer that the from and/or reply to address contain the email of the for submitter in order to reply to the appropriate email. The following script is contained in my Google spreadsheets. Your support in resolving this issue is greatly appreciated.

function sendEmail() {
var formSheet = SpreadsheetApp.getActiveSheet();
var firstRow = 2; //Skip a row for column labels
var lastForm = formSheet.getLastRow();
var lastCol = formSheet.getLastColumn();
var flagCol = null;
var subject = formSheet.getName() + " Form";
var mailTo = "";
var collaborators = SpreadsheetApp.getActiveSpreadsheet().getCollaborators();


GmailApp.sendEmail('ryan.potter@promedeq.com', subject + formVals[1], message, {"htmlBody": html});
form.getCell(1, flagCol+1).setValue(true);
SpreadsheetApp.flush(); // Make sure the cell is updated right away in case the script is interrupted
}
}
}

Cross Browser XMLHttpRequest Explained

Modern web browsers (Chrome, Firefox, IE7+, Opera, and Safari), include a native XMLHttpRequest object for creating AJAX requests. However, we often want to support IE6 as well, and that requrires using an ActiveXObject instead of a native XMLHttpRequest object (note, IE5 and IE5.5 are obsolete and should no longer be supported). But there is often some confusion revolving around which version(s) of MSXML to use to support IE6.
Microsoft has release several versions of MSXML, but only 2 of those versions (6.0 and 3.0) should be used. According to Microsoft [blogs.msdn.com], 6.0 has the best security, performance, reliability, and W3C conformance, and 3.0 is the preferred "fallback". So, we can now define our function which will return an instance of an XMLHttpRequest object (or the ActiveXObject equivalent for IE6):

/** 
 * Gets an XMLHttpRequest. For Internet Explorer 6, attempts to use MSXML 6.0,
 * then falls back to MXSML 3.0.
 * Returns null if the object could not be created. 
 * @return {XMLHttpRequest or equivalent ActiveXObject} 
 */ 
function getXHR() { 
  if (window.XMLHttpRequest) {
    // Chrome, Firefox, IE7+, Opera, Safari
    return new XMLHttpRequest(); 
  } 
  // IE6
  try { 
    // The latest stable version. It has the best security, performance, 
    // reliability, and W3C conformance. Ships with Vista, and available 
    // with other OS's via downloads and updates. 
    return new ActiveXObject('MSXML2.XMLHTTP.6.0');
  } catch (e) { 
    try { 
      // The fallback.
      return new ActiveXObject('MSXML2.XMLHTTP.3.0');
    } catch (e) { 
      alert('This browser is not AJAX enabled.'); 
      return null;
    } 
  } 
} 

If you don't care about error handling or whether IE6 users will use MSXML 6.0 and you're perfectly content with IE6 users using the stable (though perhaps not as efficient and high-performing) MSXML 3.0, then you can shorten your code to this:


/** 
 * Gets an XMLHttpRequest. For Internet Explorer 6, attempts to use MXSML 3.0.
 * @return {XMLHttpRequest or equivalent ActiveXObject} 
 */ 
function getXHR() { 
  return window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');