Skip to content
This repository has been archived by the owner on Aug 31, 2021. It is now read-only.

Commit

Permalink
Merge branch 'dev-remaining'
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesMowery committed May 10, 2015
2 parents a6668dc + 617a5f8 commit 59b81b7
Showing 1 changed file with 60 additions and 6 deletions.
66 changes: 60 additions & 6 deletions Google Sheets/ootp-utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,103 @@ function onOpen() {
.addItem('Create Salary Totals', 'addSalaries')
.addSeparator()
.addItem('Create Budget Estimates', 'addBudgets')
.addItem('Create Remaining Budget', 'remainingBudget')
.addSeparator()
.addItem('Show Options', 'showSidebar')
.addToUi();
}

/**
* Displays the remaining budget by subtracting budget from salary
*/
function remainingBudget() {
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getDataRange().getValues();
var ui = SpreadsheetApp.getUi();

var hasBudget = false;
var budgetTerm = "BUDGET";
var i, j;
var response = "";
var lastRow;
var currentRow;
var numberFormat = "$#,##0_)";

// Check to see if budget estimates have been rendered
for (i in data) {
for (j in data[i]) {
// If the budgetTerm is found, continue
if (String(data[i][j]).search(budgetTerm) !== -1) {
currentRow = Number(i) + 1;
hasBudget = true;
}
}
}

// If the budgetTerm isn't found, ask the user to create one or cancel
if (hasBudget === false) {

response = ui.alert("No budget has been created.",
"Would you like to create a budget now? \
\(If not, this operation will cancel.\)",
ui.ButtonSet.YES_NO);
// Create the budgets, or stop the operation, depending on the response
if (String(response) === "YES") {
addBudgets();
}
else if (String(response) === "NO") {
return null;
}

// Select the last row
currentRow = Number(sheet.getDataRange().getHeight()) + 1;
}

sheet.getRange(currentRow + 1, 1).setValue("REMAINING");
sheet.getRange(currentRow + 1, 2, 1,
Number(sheet.getDataRange().getWidth()) - 1)
.setValue(Utilities.formatString('=MINUS(B%s, B%s)', currentRow, currentRow - 1))
.setNumberFormat(numberFormat);
}

/**
* Prompts the user to enter budgets and returns those values
*/
function getBudgets() {
var ui = SpreadsheetApp.getUi();

var result = null;
var budgets = {
'last': 0,
'current': 0,
'next': 0,
'two': 0
};

// Prompt the user for last year's budget
result = ui.prompt(
'Let\'s set up your budgets!',
'What was your previous year\'s budget?',
ui.ButtonSet.OK_CANCEL
);
budgets.last = Number(result.getResponseText());

// Prompt the user for this year's budget
result = ui.prompt(
'This Year',
'What is this year\'s projected budget?',
ui.ButtonSet.OK_CANCEL
);
budgets.current = Number(result.getResponseText());

// Prompt the user for next year's projected budget
result = ui.prompt(
'Next Year',
'What is your budget projected to be next year?',
ui.ButtonSet.OK_CANCEL
);
budgets.next = Number(result.getResponseText());

// Prompt the user for the projected budget in two years
result = ui.prompt(
'Two Years Ahead',
Expand Down Expand Up @@ -83,7 +137,7 @@ function getBudgets() {
*/
function addBudgets() {
var sheet = SpreadsheetApp.getActiveSheet();

var budgetTerm = "BUDGET";
var selectedTermBudget = false;
var currentRow = 0;
Expand Down

0 comments on commit 59b81b7

Please sign in to comment.