Я немного опытен в программировании в целом, и это мой первый скрипт Служб Google.
Я понял, что мне нужно асинхронно вызывать getCapitalOneEmails()
, getChaseEmails()
и getBofAEmails()
,поэтому я переделал код на то, что я включил ниже.Затем я запускаю getEmails()
с помощью временного триггера.И, поскольку GAS не поддерживает ES6, я скомпилировал код для ES3, используя закрывающий компилятор Google .Скомпилированный скрипт не дает мне никаких ошибок в редакторе GAS, но он все еще не работает.
Сценарий, который я собрал ниже, прокомментирован повсюду, поэтому я не буду объяснять, что он делает здесь.
РЕДАКТИРОВАТЬ : После реализации мне на самом деле не нужны функции для запуска асинхронного (они ищут разные метки электронной почты, поэтому им не нужнычтобы следовать друг за другом), я упростил функцию getEmails()
, ниже.Теперь нет никаких ошибок из-за ES6, но данные, кажется, отсутствуют.Финальный addDataToSpreadsheet()
срабатывает, но данные не вводятся.Будет ли это потому, что я использую те же имена переменных для данных, передаваемых в addDataToSpreadsheet()
?
/*
* This Google Apps Script is running inside my selected
* spreadsheet, so any reference to getActiveSpreadsheet() method
* will create a new sheet within that context.
*
*/
// This function creates a new monthly sheet
function createMonthlySheet() {
// Get a new formatted date object
var currentMonthYear = Utilities.formatDate(new Date(), 'GMT-5', 'MM/yyyy');
// Insert formatted date string from above
var sheetName = currentMonthYear;
var ss = SpreadsheetApp.getActiveSpreadsheet();
// Create the new sheet
ss.insertSheet(sheetName, 0);
// Now get the newly-create currently active sheet
var newSheet = ss.getActiveSheet();
// Get all sheets
var allSheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
// Get the next oldest sheet
var oldSheet = allSheets[1];
// Hide the old one
oldSheet.hideSheet();
// Set our monthly budget amount
var budgetAmt = 1510;
// Set H1 to the active cell
var budgetCell = newSheet.setActiveSelection('H1');
// Set H1 to our monthly budget
budgetCell.setValue(budgetAmt);
// Set the number format to US-style currency
budgetCell.setNumberFormat('$#,##0.00');
// Set F1 as the active cell
var sumCell = newSheet.setActiveSelection('F1');
// Set the formula to the sum of column D
sumCell.setFormula('=SUM(D:D)');
// Set L1 as the active cell
var diffCell = newSheet.setActiveSelection('L1');
// Set the formula to the sum of budgetCell - sumCell
diffCell.setFormula('=SUM(H1,-(F1))');
// Set the number format to US-style currency
diffCell.setNumberFormat('$#,##0.00');
// Get column D
var range = newSheet.getRange('D:D');
// Set column D as the active range
var moneyCol = newSheet.setActiveRange(range);
// Set the number format to US-style currency
moneyCol.setNumberFormat('$#,##0.00');
}
// This function fires different functions for each email label
function getEmails() {
getCapitalOneEmails();
getChaseEmails();
getBofAEmails();
}
// This function fetches CapitalOne transaction emails and parses the data
function getCapitalOneEmails() {
// Set a variable to our "CapitalOne Transaction" label in Gmail
var label = GmailApp.getUserLabelByName('CapitalOne Transaction');
if (label) {
var threads = label.getThreads(); // Get threads of label above
for (var i in threads) {
var messages = threads[i].getMessages(); // Get messages in threads
for (var j in messages) {
if (messages[j].isUnread()) {
// Only get unread messages
var emailBody = messages[j].getPlainBody(); // Get email body in plaintext, no HTML
Logger.log('Email body: ' + emailBody);
// This sets out data variables to an empty string initially
var message_account = '',
message_date = '',
message_vendor = '',
message_amount = '';
// Get account number
var regExpAcct = /RE\: Account ending in (\d{4})/; // regex to find 4-digit account number
var CO_message_account = regExpAcct.exec(emailBody);
if (CO_message_account) {
message_account = CO_message_account[1]; // Insert Group 1 got data from regex execution to variable
Logger.log('Email message accnt: ' + message_account);
}
// Get date of transaction
var regExpDate = /we're notifying you that on (...+), at/; // regex to find date
var CO_message_date = regExpDate.exec(emailBody);
if (CO_message_date) {
message_date = CO_message_date[1]; // Insert Group 1 got data from regex execution to variable
Logger.log('Email message date: ' + message_date);
}
// Get vendor name
var regExpVendor = /, at (...+),/; // regex to find transaction vendor name
var CO_message_vendor = regExpVendor.exec(emailBody);
if (CO_message_vendor) {
message_vendor = CO_message_vendor[1]; // Insert Group 1 got data from regex execution to variable
Logger.log('Email message vendor: ' + message_vendor);
}
// Get transaction amount
var regExpAmount = /purchase in the amount of \$(\S+) was/; // regex to find transaction amount
var CO_message_amount = regExpAmount.exec(emailBody);
if (CO_message_amount) {
message_amount = CO_message_amount[1]; // Insert Group 1 got data from regex execution to variable
Logger.log('Email message amount: ' + message_amount);
}
// Calls the function, below, that adds the data to the spreadsheet, with our data inserted as parameters
addDataToSpreadsheet(
message_date,
message_account,
message_vendor,
message_amount
);
messages[j].markRead(); // Mark the message as read to end
}
}
}
} // End if(label)
}
// This function fetches Chase(Amazon) transaction emails and parses the data
function getChaseEmails() {
// Set a variable to our "Chase Transaction" label in Gmail
var label = GmailApp.getUserLabelByName('Chase Transaction');
if (label) {
var threads = label.getThreads(); // Get threads of label above
for (var i in threads) {
var messages = threads[i].getMessages(); // Get messages in threads
for (var j in messages) {
if (messages[j].isUnread()) {
// Only get unread messages
var emailBody = messages[j].getPlainBody(); // Get email body in plaintext, no HTML
Logger.log('Email body: ' + emailBody);
// This sets out data variables to an empty string initially
var message_account = '',
message_date = '',
message_vendor = '',
message_amount = '';
// Get account number
var regExpAcct = /help you manage your credit card account ending in (\d{4})/; // regex to find 4-digit account number
var CH_message_account = regExpAcct.exec(emailBody);
if (CH_message_account) {
message_account = CH_message_account[1]; // Insert Group 1 got data from regex execution to variable
Logger.log('Email message accnt: ' + message_account);
}
// Get date of transaction
var regExpDate = /we're notifying you that on (...+), at/; // regex to find date
var CH_message_date = regExpDate.exec(emailBody);
if (CH_message_date) {
message_date = CH_message_date[1]; // Insert Group 1 got data from regex execution to variable
Logger.log('Email message date: ' + message_date);
}
// Get vendor name
var regExpVendor = /has been authorized on (...+) EST./; // regex to find transaction vendor name
var CH_message_vendor = regExpVendor.exec(emailBody);
if (CH_message_vendor) {
message_vendor = CH_message_vendor[1]; // Insert Group 1 got data from regex execution to variable
Logger.log('Email message vendor: ' + message_vendor);
}
// Get transaction amount
var regExpAmount = /This charge of \($USD\) (\S+) at /; // regex to find transaction amount
var CH_message_amount = regExpAmount.exec(emailBody);
if (CH_message_amount) {
message_amount = CH_message_amount[1]; // Insert Group 1 got data from regex execution to variable
Logger.log('Email message amount: ' + message_amount);
}
// Calls the function, below, that adds the data to the spreadsheet, with our data inserted as parameters
addDataToSpreadsheet(
message_date,
message_account,
message_vendor,
message_amount
);
messages[j].markRead(); // Mark the message as read to end
}
}
}
} // End if(label)
}
// This function fetches Bank of America transaction emails and parses the data
function getBofAEmails() {
// Set a variable to our "BofA Transaction" label in Gmail
var label = GmailApp.getUserLabelByName('BofA Transaction');
if (label) {
var threads = label.getThreads(); // Get threads of label above
for (var i in threads) {
var messages = threads[i].getMessages(); // Get messages in threads
for (var j in messages) {
if (messages[j].isUnread()) {
// Only get unread messages
var emailBody = messages[j].getPlainBody(); // Get email body in plaintext, no HTML
Logger.log('Email body: ' + emailBody);
// This sets out data variables to an empty string initially
var message_account = '',
message_date = '',
message_vendor = '',
message_amount = '';
// Get account number
var regExpAcct = /RE\: Account ending in (\d{4})/; // regex to find 4-digit account number
var BA_message_account = regExpAcct.exec(emailBody);
if (BA_message_account) {
message_account = BA_message_account[1]; // Insert Group 1 got data from regex execution to variable
Logger.log('Email message accnt: ' + message_account);
}
// Get date of transaction
var regExpDate = /we're notifying you that on (...+), at/; // regex to find date
var BA_message_date = regExpDate.exec(emailBody);
if (BA_message_date) {
message_date = BA_message_date[1]; // Insert Group 1 got data from regex execution to variable
Logger.log('Email message date: ' + message_date);
}
// Get vendor name
var regExpVendor = /, at (...+),/; // regex to find transaction vendor name
var BA_message_vendor = regExpVendor.exec(emailBody);
if (BA_message_vendor) {
message_vendor = BA_message_vendor[1]; // Insert Group 1 got data from regex execution to variable
Logger.log('Email message vendor: ' + message_vendor);
}
// Get transaction amount
var regExpAmount = /purchase in the amount of \$(\S+) was/; // regex to find transaction amount
var BA_message_amount = regExpAmount.exec(emailBody);
if (BA_message_amount) {
message_amount = BA_message_amount[1]; // Insert Group 1 got data from regex execution to variable
Logger.log('Email message amount: ' + message_amount);
}
// Calls the function, below, that adds the data to the spreadsheet, with our data inserted as parameters
addDataToSpreadsheet(
message_date,
message_account,
message_vendor,
message_amount
);
messages[j].markRead(); // Mark the message as read to end
}
}
}
} // End if(label)
}
function addDataToSpreadsheet(date, account, vendor, amount) {
var rowData = [date, account, vendor, amount];
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var lock = LockService.getScriptLock();
lock.waitLock(30000);
try {
sheet
.insertRowBefore(2)
.getRange(2, 1, 1, rowData.length)
.setValues([rowData]);
SpreadsheetApp.flush();
} finally {
lock.releaseLock();
}
var range = sheet.getRange('D:D');
var moneyCol = sheet.setActiveRange(range);
moneyCol.setNumberFormat('$#,##0.00');
}