Skip to content

Instantly share code, notes, and snippets.

@timba64
Last active November 26, 2018 09:24
Show Gist options
  • Save timba64/2855b57abeff72a00315593bf253cee7 to your computer and use it in GitHub Desktop.
Save timba64/2855b57abeff72a00315593bf253cee7 to your computer and use it in GitHub Desktop.
Create custom menu in google spreadsheet, get unread email from google account and put it in google sheets
function onOpen(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.addMenu("Меню для Оли",
[{ name: "Write to file", functionName: "writeToFile" },
{ name: "Remove Duplicates", functionName: "removeDuplicates" },
{ name: "Count lids", functionName: "countLids" }]
);
}
/**
* This function write some emails form gmail into our sheet
*
*/
function writeToFile() {
var sheet = SpreadsheetApp.getActiveSheet();
//var query = 'is:read subject:"Елизинг"';
var query = 'is:unread from:wordpress@e-lizing.by ';
threads = GmailApp.search(query);
//sheet.appendRow(['Status', 'Date', 'Phone', 'Theme', 'Message'] ); add row with cells
for (var x=0; x<threads.length; x++) {
var messages = threads[x].getMessages();
for (i=0;i<=messages.length-1;i++) {
var mess = messages[i];
var ind = mess.getPlainBody().indexOf("Телефон:") + 9;
var phone = "'" + mess.getPlainBody().substr(ind, 19);
sheet.appendRow(['', mess.getDate(), phone, mess.getSubject(), mess.getPlainBody()]);
}
}
}
/**
* Removes duplicate rows from the current sheet.
*/
function removeDuplicates() {
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getDataRange().getValues();
var newData = [];
for (i in data) {
var row = data[i];
var duplicate = false;
for (j in newData) {
if (row.join() == newData[j].join()) {
duplicate = true;
}
}
if (!duplicate) {
newData.push(row);
}
}
sheet.clearContents();
sheet.getRange(1, 1, newData.length, newData[0].length).setValues(newData);
}
/**
* Count lids in google spreadsheet
*/
function countLids() {
var d = new Date();
var count = 0;
var sheet = SpreadsheetApp.getActiveSheet();
//var last = sheet.getDataRange().getLastRow();
//var data = sheet.getRange('A2:' + last).getValues();
var data = sheet.getDataRange().getValues();
for (var i = 0; i < data.length; i++) {
if(data[i][0] == 'лид') { //check what place in first column
count++;
}
}
var serv = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("service"); // go on the sheet 'service'
serv.activate();
// variant - 1
var last = serv.getDataRange().getLastRow() + 1; // the number of the last row
//var rangeCell = serv.getRange( 'A' + last );
//rangeCell.activate();
//rangeCell.setValue(count);
// variant - 2
serv.appendRow([count, ' - количество лидов на - ', formatDate(d)]);
}
/**
* format date
*/
function formatDate(date) {
var dd = date.getDate();
if (dd < 10) dd = '0' + dd;
var mm = date.getMonth() + 1;
if (mm < 10) mm = '0' + mm;
var yy = date.getFullYear() % 100;
if (yy < 10) yy = '0' + yy;
return dd + '.' + mm + '.' + yy;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment