Created
December 3, 2019 13:14
-
-
Save tariqhamid/5024ee696513e9d828ad2e59faf87289 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function insertTransaction(dateString, description, amount) { | |
var result = { | |
success: false, | |
message: null | |
}; | |
try { | |
var budgetSpreadsheetId = '<INSERT-SPREADSHEET-ID-HERE>'; | |
// Open the spreadsheet | |
var spreadsheet = SpreadsheetApp.openById(budgetSpreadsheetId); | |
// Determine which sheet to open based on the month of the transaction | |
var sheetName = getSheetName(dateString); | |
var sheet = spreadsheet.getSheetByName(sheetName); | |
// Append the new transaction to the table | |
sheet.appendRow([dateString, description, amount]); | |
result.success = true; | |
result.message = "Transaction added."; | |
} | |
catch (e) { | |
result.success = false; | |
result.message = e.message; | |
} | |
finally { | |
return JSON.stringify(result); | |
} | |
} | |
function getSheetName(dateString) { | |
var sheetNames = [ | |
"January Transactions", | |
"February Transactions", | |
"March Transactions", | |
"April Transactions", | |
"May Transactions", | |
"June Transactions", | |
"July Transactions", | |
"August Transactions", | |
"September Transactions", | |
"October Transactions", | |
"November Transactions", | |
"December Transactions", | |
]; | |
// Get the month number, then subtract one to get the 0-indexed value from the sheetNames array | |
var monthNumber = parseInt(dateString.substring(5, 7)); | |
return sheetNames[monthNumber - 1]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment