-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomerData.js
More file actions
235 lines (217 loc) · 6.7 KB
/
Copy pathCustomerData.js
File metadata and controls
235 lines (217 loc) · 6.7 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/**
* Returns a 2 dimensional array of names for use in a menu
*/
function CustomerNames(){
let ss = SpreadsheetApp.getActiveSpreadsheet();
let customerNames = ss.getRangeByName('CUSTOMER_NAMES').getDisplayValues();
// console.log(customerNames.map(function(name){if(name != ''){return name}}));
// let customerList = customerNames.filter((customerName) => customerName[0].length > 0)
// console.log(customerNames)
let customerList = _getUniqueList(customerNames);
console.log(customerList)
return customerList
};
/**
* Customer Selection list
*
*/
function CustomerSelectionList(){
let customerList = CustomerNames();
customerList.unshift("New Customer");
// console.log(customerList)
return customerList
}
/**
* Gets the names of the days customers and updates the list
*/
function DailyCustomerList(){
let ss = SpreadsheetApp.getActiveSpreadsheet();
let ls = ss.getSheetByName(GetMonday());
let nameList = ls.getRange(4,3,ls.getLastRow()-3,1).getValues();
// console.log("Name List before filter: ");
// console.log(nameList);
let names = _getUniqueList(nameList);
// console.log("_getUniqueList complete,");
// console.log("Returning names from DailyCustomerList"+names);
return names
};
/**
* Merges company name List with new name list
*
* @param {array} company
* @return {array}
*/
function UpdateCustomerList(company){
let oneCustomer = company;
let ss = SpreadsheetApp.getActiveSpreadsheet();
let ds = ss.getSheetByName('Data');
let r = ss.getRangeByName('CUSTOMER_NAMES');
let newCustomers = DailyCustomerList();
let newList = CustomerNames();
// let oldList = CustomerNames();
// for(let i = 0;i<newCustomers.length;i++){
// newList.unshift(newCustomers[i])
// }
if(oneCustomer){
if(!newList.find((item) => item == oneCustomer)){
newList.unshift(oneCustomer);
}
} else{
newCustomers.forEach(function(customer){
if(!newList.find((item) => item == customer)){
newList.unshift(customer)}})
};
// console.log("New List before Transposing"+newList);
let newListArr = _transposeArray(newList)
// console.log("NewListArr is newList Transposed:");
// console.log(newListArr);
// console.log("newListArr.length Call after this Log : ");
let numRows = newListArr.length;
// console.log(numRows);
r.clear;
r = ds.getRange(2,1,numRows,1);
r.setValues(newListArr);
ss.setNamedRange('CUSTOMER_NAMES',r);
return newList
}
/**
* Gets the machines associated with the chosen customer
*
*/
function GetMachines(name){
let assetList=[];
if(name === "New Customer"){
assetList.unshift("It's sending New Customer");
return assetList
} else {
assetList = GetClientAssetList(name);
}
console.log(assetList);
if(assetList.indexOf(name)>=0){
assetList.shift();}
assetList.push("New Machine")
assetList.unshift('');
console.log(assetList);
return assetList
}
/**
* This should return a list of all machines shown to the
* right of the company name on the Data page
*/
function DailyMachineList(name){
let ss = SpreadsheetApp.getActiveSpreadsheet();
let logSheet = ss.getSheetByName(GetMonday());
let machList = logSheet.getRange(4,3,logSheet.getLastRow()-3,2).getValues();
// console.log(nameList)
let machines = machList.filter((name) => name.toString.length>0);
// console.log("name : "+names);
// console.log(names);
return machines
}
/**
* Update the Customers Machine list
*/
function UpdateCustomersMachines(customer, machine){
// console.log(customer)
let cusName = customer;
let ss = SpreadsheetApp.getActiveSpreadsheet();
let ds = ss.getSheetByName('Data');
// console.log(cusName);
let machines = GetClientAssetList(cusName);
// console.log(machines);
machines.push(machine)
let range = ss.getRangeByName('CUSTOMER_MACHINES').getDisplayValues();
let position;
for(let i=0;i<range.length;i++){
if(range[i].indexOf(machines[0])>=0){
position = i+2;}
};
let values = [];
values.push(machines)
// console.log(position);
// console.log(values);
let length = machines.length;
// console.log(length);
ds.getRange(position,3,1,length).setValues(values)
}
/**
* returns the row index of the customer data
*/
function GetCusRecords(customer,machine){
// console.log(customer)
let ss = SpreadsheetApp.getActiveSpreadsheet();
let dataSheet = ss.getSheetByName('Data');
let nameRange = ss.getRangeByName('CUSTOMER_NAMES')
let names = nameRange.getDisplayValues();
// console.log("names: "+names);
// console.log("names.length: "+names.length);
// console.log("customer.name: "+customer.name)
let customerList = names.filter((name) => name[0].length > 0);
// console.log(customerList);
let row = customerList.findIndex((cust) => cust[0] === customer.name);
// console.log("row: "+row);
let lastCol = customer.machines.length + 1;
if(row >= 0){
customer.row = row+2
let machines = dataSheet.getRange(customer.row,2,1,lastCol).getDisplayValues()
let machList = machines.filter((machineName) => machineName[0].length > 0)
machList[0].unshift(machine);
machList[0].unshift(customer.name);
// console.log("machList "+machList);
customer.column = machList[0].length;
customer.machines = new Array(machList[0])
// console.log(customer)
};
let newMachRange = dataSheet.getRange(customer.row,1,1,customer.column)
newMachRange.setValues(customer.machines);
return customer
}
function GetClientAssetList(name){
let ss = SpreadsheetApp.getActiveSpreadsheet();
let ds = ss.getSheetByName('Data');
name = name.trim();
let cusRecords = ss.getRangeByName('CUSTOMER_MACHINES');
let customerData = cusRecords.getValues();
console.log(customerData);
let machines = [];
try{for(let i=1;i<customerData.length;i++) {
if(customerData[i][0] === name){
machines.push(customerData[i]);
};
};
}catch{
let message = "Failure at the array building the data given is "+customerData;
return message
}
let assetList = _getUniqueList(machines);
console.log("after Get Unique List");
console.log(assetList[0])
return assetList
}
/**
* Test UpdateMachines
*/
function UpdateCustomerInfo(){
let customer = {
name : "Master Doors",
row : "",
column : "",
machines : []
}
let machine = "Hammer"
let cusInfo = GetCusRecords(customer,machine);
console.log(cusInfo+" Logged")
}
/**
* Adds a row of data to the current active log
*/
function TestNewList(){
let newCustomers = "Gypsy";
// newCustomers.push(["Tom"]);
// newCustomers.push(["Dan"]);
// newCustomers.push(["Pete"]);
console.log("New Customers Array being passed =");
console.log(newCustomers);
console.log("Updated Customer List = ");
UpdateCustomerList(newCustomers)
}