-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
179 lines (145 loc) · 6.07 KB
/
scripts.js
File metadata and controls
179 lines (145 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
* Considerations:
* - How to tell if agency is editing, or if it is a duplicate agency name?
* - What to do if agency edits? Replace data or create another row?
* - BugFix: "indexOf" will not work for Sheet object. Either get the name value out of the sheet object, or get array of strings that are the sheet names to compare
* - What data can the trigger functions pass to us?
* - Look into trigger events, compare pros/cons of using UI trigger vs creating Trigger with code
* - Test if the new transpose function works correctly
*/
// Global variables
const questionsLength = 23;
const primarySheet = "mainSheet"
function main() {
//Creates mainsheet variable. Used getSheetByName because getActiveSpreadsheet() will change after a new tab is added
let mainSheet = SpreadsheetApp.getActive().getSheetByName(primarySheet);
// Get a list of all exisitingAgencies (ie agencies with their own tab)
// returns array of Sheet objects
let existingAgencies = getExistingAgencies();
// Get all agency names in the main sheet
// returns array of strings
let allNames = mainSheetNames(mainSheet);
// Get a list names which are on the main sheet but aren't tabs
// returns array of strings
let newAgencies = newAdditions(allNames, existingAgencies);
// Create new tabs/pages for newly added agencies
createAgencyTabs(newAgencies, mainSheet)
// newAgencies.forEach(agency => {
// createAgencyTab(agency, mainSheet)
// })
}
// Gets a list of Agency names for all agencies that already have their own tab
function getExistingAgencies(){
// Reference to all current sheets, returns array of all sheets in the spreadsheet
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
// return a slice of the sheets that is all but the first sheet
var existingAgencies = sheets.slice(1);
// // iterate through each sheet name and add it to the list
// sheets.forEach(function(sheet,index) {
// var agencyName = sheet.getName();
// //ignore the name of the mainsheet (which is at index 0.0). may be better to use main sheet rather than index
// if(index != 0){
// existingAgencies.push(agencyName)
// }
// })
return existingAgencies;
}
// Returns names of every agency on the main sheet
function mainSheetNames(mainSheet){
var mainSheetNames = [];
var rowLevel = 2;
var agencyNameColumnIndex = 3;
// values is currently undefined so we enter the loop
while(values != ""){
// getRange(row, column, numRows, numColumns)
var range = mainSheet.getRange(rowLevel, agencyNameColumnIndex, 1, 1);
var values = range.getValues();
if(values[0][0]){
mainSheetNames.push(values[0][0]) // is values[0][0] so that we are adding just the name and not the nested list
}
rowLevel++;
}
return mainSheetNames;
}
// Get a list of all newly added agencies (agencies that are on the main sheet but don't have tabs made)
function newAdditions(allAgencies, existingAgencies){
let newAgencies = [];
allAgencies.forEach(function(agency) {
// If the agency name is not in existingAgencies list, add it to the newAgencies list
if(existingAgencies.indexOf(agency[0]) == -1) {
newAgencies.push(agency);
}
})
return newAgencies;
}
function createAgencyTabs(newAgencies, mainSheet) {
var rowLevel = 2; // we want to start at row 2 (first row after the row headings)
var range = mainSheet.getRange(rowLevel, 1, 1, questionsLength); // defines dimensions of row
var values = range.getValues();
// While loop will continute to execute until there are no more entries
while(values[0][2]){
range = mainSheet.getRange(rowLevel, 1, 1, questionsLength);
values = range.getValues();
// if this row is one of the new agencies (values[0][2] is agency name) create a new tab and add the data
if(newAgencies.indexOf(values[0][2]) > -1){
createNewPage(values[0][2])
var newPage = SpreadsheetApp.getActive().getSheetByName(values[0][2])
// Set current range to the top 23 cells
var pageRange = newPage.getRange(1,1,1, questionsLength);
// Add column titles to the new page
pageRange.setValues(getColumnTitles(mainSheet));
// Change range to the next row down
pageRange = newPage.getRange(2,1,1, questionsLength);
// now add that agencies information in the row
pageRange.setValues(values);
}
rowLevel++; // moves row level down to the next row
}
}
// Creates a single tab for the name of the agency
// agency is the name of the agency
function createAgencyTab(agency, mainSheet){
createNewPage(agency);
var newPage = SpreadsheetApp.getActive().getSheetByName(agency)
if(!newPage) return;
// Set current range to the top 23 cells
var pageRange = newPage.getRange(1,1,1, questionsLength);
var columnTitles = getColumnTitles(mainSheet)
// Add column titles to the new page
pageRange.setValues(columnTitles);
// Change range to the next row down
pageRange = newPage.getRange(2,1,1, questionsLength);
// now add that agencies information in the row
pageRange.setValues(values);
}
// UTILITY FUNCTIONS
/**
* Objectives:
* - transpose data in a row so that it appears as a column
*/
function transposeRow() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Why "21"? It was "23" in an earlier function
var range = sheet.getRange(`A1:B${questionsLength}`);
var chart = sheet.newChart()
.setChartType(Charts.ChartType.BAR)
.addRange(range)
.setTransposeRowsAndColumns(true)
.setPosition(5, 5, 0, 0)
.build();
sheet.insertChart(chart);
}
function createNewPage(name) {
var activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var yourNewSheet = activeSpreadsheet.insertSheet();
yourNewSheet.setName(name);
}
// Gets column titles from the main page.
function getColumnTitles(mainSheet) {
var range = mainSheet.getRange(1, 1, 1, questionsLength);
var titles = range.getValues();
return titles;
}
// getRange(row, column, numRows, numColumns)
// Returns the range with the top left cell at the given coordinates with the given number of rows and columns.