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

Commit

Permalink
Code cleanup for consistency and clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesMowery committed May 9, 2015
1 parent ea70bd8 commit a6668dc
Showing 1 changed file with 64 additions and 51 deletions.
115 changes: 64 additions & 51 deletions Google Sheets/ootp-utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,45 @@ function onOpen() {
*/
function getBudgets() {
var ui = SpreadsheetApp.getUi();

var result;
// var button, text


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

'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);
'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);
'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);
'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',
'What is your budget projected to be in two years?',
ui.ButtonSet.OK_CANCEL);
'Two Years Ahead',
'What is your budget projected to be in two years?',
ui.ButtonSet.OK_CANCEL
);
budgets.two = Number(result.getResponseText());

/*
Expand All @@ -76,35 +82,36 @@ function getBudgets() {
* by using Google Sheet's TREND formula.
*/
function addBudgets() {

var sheet = SpreadsheetApp.getActiveSheet();

var budgetTerm = "BUDGET";
var selectedTermBudget = false;
var currentRow = 0;
var currentCol = 0;
var numberFormat = "$#,##0_)";
var data = null;
var i = 0, j = 0;
var budgets = null;

// If selected cell contains budgetTerm,
// insert TREND formula to calculate future budgets
if (sheet.getActiveCell().getValue() == budgetTerm) {
if (sheet.getActiveCell().getValue() === budgetTerm) {
// TODO: If selected cell contains budgetTerm
// expand formulas to the right
var data = sheet.getDataRange().getValues();
data = sheet.getDataRange().getValues();
}
else {
var data = sheet.getDataRange().getValues();
data = sheet.getDataRange().getValues();
}

// Search for the term denoting the budget row
for (var i in data) {
for (var j in data[i]) {
for (i in data) {
for (j in data[i]) {

// If the budgetTerm is found, store the cell
if (String(data[i][j]).search(budgetTerm) !== -1) {
currentRow = Number(i) + 1;
currentCol = Number(j);

}
}
}
Expand All @@ -117,7 +124,7 @@ function addBudgets() {
}

// Retrieve the budgets from the user
var budgets = getBudgets();
budgets = getBudgets();

// Set the budget values in the cells
sheet.getRange(currentRow, 2).setValue(budgets.last);
Expand All @@ -127,14 +134,17 @@ function addBudgets() {

// Set the TREND formula for the remaining columns
sheet.getRange(currentRow, 6)
.setValue(Utilities
.formatString('=TREND(B%s:E%s, B1:E1, F1:K1)',
currentRow, currentRow));
.setValue(
Utilities.formatString(
'=TREND(B%s:E%s, B1:E1, F1:K1)',
currentRow, currentRow
)
);

// Set the number formats for the column
sheet.getRange(currentRow, 2, 1, sheet.getDataRange().getWidth() - 1)
.setNumberFormat(numberFormat);

sheet.getRange(currentRow, 2, 1,
sheet.getDataRange().getWidth() - 1
).setNumberFormat(numberFormat);
}

/**
Expand All @@ -150,22 +160,23 @@ function addSalaries() {
var currentRow = 0;
var currentCol = 0;
var numberFormat = "$#,##0_)";
var data = null;
var i, j;

// If selected cell contains totalTerm, expand SUM formulas,
// otherwise collect all data and search for totalTerm
if (sheet.getActiveCell().getValue() == totalTerm) {
if (sheet.getActiveCell().getValue() === totalTerm) {
// TODO: If selected cell contains totalTerm
// expand formulas to the right
}
else {
var data = sheet.getDataRange().getValues();
data = sheet.getDataRange().getValues();
}

// Search for the term denoting the totals row
for (var i in data) {
for (var j in data[i]) {
for (i in data) {
for (j in data[i]) {
if (String(data[i][j]).search(totalTerm) !== -1) {

// When found, store the row and column values
currentRow = Number(i);
currentCol = Number(j);
Expand All @@ -192,17 +203,19 @@ function cleanSalaries() {
var leadingSlice = 0;
var leadingNumber = "";
var numberFormat = "$#,##0_)";

var rangeSelectorEnabled = false;
var data = null;
var i, j;

// TODO: Detect if custom range selection is enabled via dialog box

if (rangeSelectorEnabled === true) {
// var data = SpreadsheetApp.getActiveSheet().getActiveRange().getValues();
// data = SpreadsheetApp.getActiveSheet().getActiveRange().getValues();
}
else {
var data = sheet.getRange(2, 2, sheet.getDataRange().getHeight() - 2,
sheet.getDataRange().getWidth() - 2).getValues();
data = sheet.getRange(2, 2, sheet.getDataRange().getHeight() - 2,
sheet.getDataRange().getWidth() - 2)
.getValues();

// Freeze the first row and column for pretty formatting
sheet.setFrozenRows(1);
Expand All @@ -211,12 +224,12 @@ function cleanSalaries() {

// Search through each cell to locate data that needs modified,
// and then apply those modifications.
for (var i = 0; i < data.length; i++) {
for (i = 0; i < data.length; i++) {

// Push an empty array to enable 2D array
result.push([]);

for (var j = 0; j < data[i].length; j++) {
for (j = 0; j < data[i].length; j++) {

// Convert data to a string for modification
cellText = String(data[i][j]);
Expand Down

0 comments on commit a6668dc

Please sign in to comment.