Skip to content

Instantly share code, notes, and snippets.

@hanguokai
Last active December 20, 2015 15:19
Show Gist options
  • Save hanguokai/6153342 to your computer and use it in GitHub Desktop.
Save hanguokai/6153342 to your computer and use it in GitHub Desktop.
统计 Google doc 电子表格的有效行数,一般用于报名表(表单回复)的人数统计,可以方便地知道当前的报名人数,并且以 email 作为唯一标示过滤重复的行。行数统计不包括电子表格中的第一行,因为第一行一般是标题而不是数据。
// email 所在的列,从0开始算。email 作为唯一标示,用于去重。
var emailColumn = 1;
// 电子表格的 doc key
key = "0AhIp-TG_44qxdHFnTDFUeDNOUEp2LVVpbmVMa25QQVE";
/**
* 统计报名人数,根据 email 唯一标示去除重复的报名。
*/
function countUniqueRows() {
var sheet = SpreadsheetApp.openById(key).getSheets()[0];
var data = sheet.getDataRange().getValues();
// a hashset for unique email
var emailSet = {};
var newData = new Array();
// 有效数据从第二行开始,跳过第一行的 title 内容
for (var i = 1; i < data.length; i++) {
var row = data[i];
var email = row[emailColumn];
if(!emailSet[email]){
emailSet[email] = true;//add email to hashset
newData.push(row);
} else {
//Logger.log('Duplicate: ' + email);
}
}
//打开日志查看结果
Logger.log('Unique Count: ' + newData.length);
Logger.log('All Count: ' + (data.length - 1));
}
@hanguokai
Copy link
Author

亲,这是 Google Apps Script ,只能在 Google 云端运行,不是 js 脚本,目前不支持其它语言。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment