WebService.ashx
<%@ WebHandler Language="C#" Class="WebService" %> using System; using System.Web; using Google.GData.Client; using Google.GData.Spreadsheets; using System.Configuration; using System.Linq; public class WebService : IHttpHandler { public void ProcessRequest (HttpContext context) { context.Response.ContentType = "text/plain"; string email = context.Request["e"]; string path = context.Request["c"]; if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(path)) { context.Response.Write("Email is not null"); } else { InsertRegstrastionDataIntoSpreadsheet(email, path); context.Response.Write("Success!!"); } } #region Google Spreadsheet Insert private SpreadsheetsService service; protected void InsertRegstrastionDataIntoSpreadsheet(string email, string path) { service = new SpreadsheetsService("GoogleHyundaiWebServiceRegistration_Spreadsheet"); service.setUserCredentials(ConfigurationManager.AppSettings["google_username"], ConfigurationManager.AppSettings["google_password"]); SpreadsheetQuery query = new SpreadsheetQuery(); SpreadsheetFeed feed = service.Query(query); SpreadsheetEntry spreadSheetEntry = null; foreach (SpreadsheetEntry entry in feed.Entries) { if (entry.Title.Text == ConfigurationManager.AppSettings["registration_spreadsheet_name"])//此处是google execl名字
{ spreadSheetEntry = entry; } } if (spreadSheetEntry != null) { AtomLink link = spreadSheetEntry.Links.FindService(GDataSpreadsheetsNameTable.WorksheetRel, null); WorksheetQuery worksheetQuery = new WorksheetQuery(link.HRef.ToString()); WorksheetFeed workSheetfeed = service.Query(worksheetQuery); WorksheetEntry worksheetEntry = workSheetfeed.Entries.First() as WorksheetEntry; InsertRow(service, worksheetEntry, email, path); } } protected ListEntry InsertRow(SpreadsheetsService service, WorksheetEntry entry, string email,string path) { AtomLink listFeedLink = entry.Links.FindService(GDataSpreadsheetsNameTable.ListRel, null); ListQuery query = new ListQuery(listFeedLink.HRef.ToString()); ListFeed feed = service.Query(query); ListEntry row = new ListEntry(); //这个地方不知道为什么非得要小写LocalName,如果有知道的请通知我下,谢谢
row.Elements.Add(new ListEntry.Custom() { LocalName = "email", Value = email }); row.Elements.Add(new ListEntry.Custom() { LocalName = "country", Value = path }); service.Insert(feed, row); return row; } #endregion public bool IsReusable { get { return false; } } }
Default.aspx
<div class="inputbox"><input name="email" type="text" class="required email"><span>Enter your email here...</span></div> <div class="submitbox"><input name="send" type="button" class="submit" value="" onClick="enter();"></div> <input id="country" type="hidden" value="china" />
main.js
var email = $('input[name="email"]').val(); var country = $('#country').val(); $.ajax({ type: "GET", url: "/handler/WebService.ashx", data: "e="+email+"&c="+country, success: function (msg) { alert(msg); } });