-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrawl.gs
351 lines (287 loc) · 12.7 KB
/
crawl.gs
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/**
* Functionalities to add:
*
* - Possibility to have relative path instead of absolute path
* - Relative path adaptable to sub-domains and different TLDs
*
**/
function crawlSheetFormat() {
// Storing the active sheet reference
let crawlSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()
// Splitting .csv file to columns
crawlSheet.getRange("A:A").splitTextToColumns()
// Storing useful ranges in variables
let crawlRange = crawlSheet.getDataRange()
let numberOfColumns = crawlRange.getNumColumns()
let numberOfRows = crawlRange.getNumRows()
let crawlFirstRow = crawlSheet.getRange("1:1")
let crawlFirstColumn = crawlSheet.getRange("A:A")
// Hiding useless columns
let columnsToHide = [{ "value": "Title 1 Length"},
{ "value": "Meta Description 1 Length"},
{ "value": "Meta Keywords 1"},
{ "value": "Meta Keywords 1 Length"},
{ "value": "H1-1 Length"},
{ "value": "H1-2"},
{ "value": "H1-2 Length"},
{ "value": "H2-1"},
{ "value": "H2-1 Length"},
{ "value": "H2-2"},
{ "value": "H2-2 Length"},
{ "value": "X-Robots-Tag 1"},
{ "value": "Meta Refresh 1"},
{ "value": 'HTTP rel="next" 1'},
{ "value": 'HTTP rel="prev" 1'},
{ "value": "amphtml Link Element"},
{ "value": "Unique JS Inlinks"},
{ "value": "% of Total"},
{ "value": "Unique JS Outlinks"},
{ "value": "Unique External JS Outlinks"},
{ "value": "Closest Similarity Match"},
{ "value": "No. Near Duplicates"},
{ "value": "Spelling Errors"},
{ "value": "Grammar Errors"},
{ "value": "Hash"},
{ "value": "Response Time"},
{ "value": "Crawl Timestamp"}]
const columnToHideHandler = (sheet, cellContent, rowIndex, columnIndex) => {
sheet.hideColumn(sheet.getRange(rowIndex,columnIndex))
}
callbackFromRowValueLookup(crawlSheet,columnsToHide,1,columnToHideHandler)
// Adding an hyperlink to the first column
let adresses = crawlFirstColumn.getValues()
let hyperlinks = []
adresses.forEach((url,index) => hyperlinks[index] = [`=HYPERLINK("${url}")`])
hyperlinks[0] = ["Address"]
crawlFirstColumn.setValues(hyperlinks)
// Freeze the first row/column
crawlSheet.setFrozenColumns(1)
crawlSheet.setFrozenRows(1)
// Apply general style
crawlRange.setHorizontalAlignment("left")
crawlRange.setVerticalAlignment("middle")
crawlRange.setWrapStrategy(SpreadsheetApp.WrapStrategy.CLIP);
crawlSheet.setRowHeightsForced(1,numberOfRows,35)
// Apply first row style
crawlFirstRow.setHorizontalAlignment("center")
crawlFirstRow.setWrapStrategy(SpreadsheetApp.WrapStrategy.WRAP);
crawlSheet.setRowHeightsForced(1,1,45)
// Modify column widths
let columnsToModifyWidth = [{"value": "Address", "width": 600},
{"value": "Content Type", "width": 150},
{"value": "Indexability Status", "width": 120},
{"value": "Title 1", "width": 200},
{"value": "Meta Description 1", "width": 200},
{"value": "H1-1", "width": 200},
{"value": "Meta Robots 1", "width": 160},
{"value": "Canonical Link Element 1", "width": 400},
{"value": "Redirect URL", "width": 400},
{"value": "URL Encoded Address", "width": 400}]
const columnToModifyWidthHandler = (sheet, cellContent, rowIndex, columnIndex) => {
sheet.setColumnWidth(columnIndex, cellContent.width)
}
callbackFromRowValueLookup(crawlSheet,columnsToModifyWidth,1,columnToModifyWidthHandler)
// Apply filter to sheet
crawlRange.createFilter()
// Add conditional formating to columns
let crawlConditionalRules = crawlSheet.getConditionalFormatRules()
// --- Status code
let statusCodeColumnIndex = getColumnIndexFromValue(crawlSheet, "Status Code", 1)
let statusCodeRange = crawlSheet.getRange(2, statusCodeColumnIndex, numberOfRows - 1, 1)
crawlConditionalRules.push(createNewConditionalFormatRule(statusCodeRange, "numberEqualTo", 404, "red"))
crawlConditionalRules.push(createNewConditionalFormatRule(statusCodeRange, "numberEqualTo", 0, "orange"))
crawlConditionalRules.push(createNewConditionalFormatRule(statusCodeRange, "numberNotEqualTo", 200, "yellow"))
// --- Indexability
let indexabilityColumnIndex = getColumnIndexFromValue(crawlSheet, "Indexability", 1)
let indexabilityRange = crawlSheet.getRange(2, indexabilityColumnIndex, numberOfRows - 1, 1)
let indexabilityStatusColumnIndex = getColumnIndexFromValue(crawlSheet, "Indexability Status", 1)
let indexabilityStatusRange = crawlSheet.getRange(2, indexabilityStatusColumnIndex, numberOfRows - 1, 1)
crawlConditionalRules.push(createNewConditionalFormatRule(indexabilityRange, "formulaSatisfied", `=IF(E2<>"Indexable",1,0)`, "yellow"))
crawlConditionalRules.push(createNewConditionalFormatRule(indexabilityStatusRange, "cellNotEmpty", "", "yellow"))
// --- Title
let titleColumnIndex = getColumnIndexFromValue(crawlSheet, "Title 1", 1)
let titleRange = crawlSheet.getRange(2, titleColumnIndex, numberOfRows - 1, 1)
let titlePixelWidthColumnIndex = getColumnIndexFromValue(crawlSheet, "Title 1 Pixel Width", 1)
let titlePixelWidthRange = crawlSheet.getRange(2, titlePixelWidthColumnIndex, numberOfRows - 1, 1)
crawlConditionalRules.push(createNewConditionalFormatRule(titleRange, "cellEmpty", "", "orange"))
crawlConditionalRules.push(createNewConditionalFormatRule(titlePixelWidthRange, "numberGreaterThan", 575, "orange"))
crawlConditionalRules.push(createNewConditionalFormatRule(titlePixelWidthRange, "numberLessThan", 280, "yellow"))
// --- Meta Description
let metaDescriptionColumnIndex = getColumnIndexFromValue(crawlSheet, "Meta Description 1", 1)
let metaDescriptionRange = crawlSheet.getRange(2, metaDescriptionColumnIndex, numberOfRows - 1, 1)
let metaDescriptionPixelWidthColumnIndex = getColumnIndexFromValue(crawlSheet, "Meta Description 1 Pixel Width", 1)
let metaDescriptionPixelWidthRange = crawlSheet.getRange(2, metaDescriptionPixelWidthColumnIndex, numberOfRows - 1, 1)
crawlConditionalRules.push(createNewConditionalFormatRule(metaDescriptionRange, "cellEmpty", "", "orange"))
crawlConditionalRules.push(createNewConditionalFormatRule(metaDescriptionPixelWidthRange, "numberGreaterThan", 920, "orange"))
crawlConditionalRules.push(createNewConditionalFormatRule(metaDescriptionPixelWidthRange, "numberLessThan", 340, "yellow"))
// --- H1
let h1ColumnIndex = getColumnIndexFromValue(crawlSheet, "H1-1", 1)
let h1Range = crawlSheet.getRange(2, h1ColumnIndex, numberOfRows - 1, 1)
crawlConditionalRules.push(createNewConditionalFormatRule(h1Range, "cellEmpty", "", "orange"))
// --- Word count
let wordCountColumnIndex = getColumnIndexFromValue(crawlSheet, "Word Count", 1)
let wordCountRange = crawlSheet.getRange(2, wordCountColumnIndex, numberOfRows - 1, 1)
crawlConditionalRules.push(createNewConditionalFormatRule(wordCountRange, "numberLessThan", 500, "orange"))
// --- Crawl depth
let crawlDepthColumnIndex = getColumnIndexFromValue(crawlSheet, "Crawl Depth", 1)
let crawlDepthRange = crawlSheet.getRange(2, crawlDepthColumnIndex, numberOfRows - 1, 1)
crawlConditionalRules.push(createNewConditionalFormatRule(crawlDepthRange, "numberGreaterThanOrEqualTo", 3, "orange"))
// --- Inlinks
let inlinksColumnIndex = getColumnIndexFromValue(crawlSheet, "Inlinks", 1)
let inlinksRange = crawlSheet.getRange(2, inlinksColumnIndex, numberOfRows - 1, 1)
let uniqueInlinksColumnIndex = getColumnIndexFromValue(crawlSheet, "Unique Inlinks", 1)
let uniqueInlinksRange = crawlSheet.getRange(2, uniqueInlinksColumnIndex, numberOfRows - 1, 1)
crawlConditionalRules.push(createNewConditionalFormatRule(inlinksRange, "numberLessThan", 10, "orange"))
crawlConditionalRules.push(createNewConditionalFormatRule(uniqueInlinksRange, "numberLessThan", 10, "orange"))
// --- Applying the rules
crawlSheet.setConditionalFormatRules(crawlConditionalRules)
// Set today's date as tab name
let tabName = new Date()
crawlSheet.setName(tabName.toLocaleDateString())
// Focus on Content-type cell to prepare filtering HTML content
crawlSheet.setCurrentCell(crawlSheet.getRange("B1"))
}
/**
* Function:
* - Return the column index by looking-up a value on a selected row
*
* Parameters:
* - Sheet
* - Value to look-up
* - Index of the row to search
*
* Return:
* - Column index of the first matched cell
*/
function getColumnIndexFromValue(sheet, value, rowIndex) {
let numberOfColumns = sheet.getDataRange().getNumColumns()
let rowRange = sheet.getRange(rowIndex,1,1,numberOfColumns)
let rowCellValues = rowRange.getValues()[0]
let rowCellValue = ""
for (let i = 0; i < rowCellValues.length; i++) {
rowCellValue = rowCellValues[i]
if (rowCellValue == value) {
return i+1
}
}
}
/**
* Function:
* - Look-up for an array of values on the contents of one row
* Performs a callback containing useful data when a result is found
*
* Parameters:
* - Sheet
* - Array of objects to look-up
* - "value" key is required (when an equal value is found, callback is called)
* - additional keys can be added to use in the callback
* - Index of the row to search
* - Callback performed when a match exists between row value and object value
*
* Return:
* - Nothing
*
* Callback parameters:
* - Sheet
* - matched Object
* - Row index of the matched cell
* - Column index of the matched cell
*/
function callbackFromRowValueLookup(sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(), cells = [], rowIndex = 1, callback) {
let numberOfColumns = sheet.getDataRange().getNumColumns()
let rowRange = sheet.getRange(rowIndex,1,1,numberOfColumns)
let rowCellValues = rowRange.getValues()[0]
let cellContent = {}
let rowCellValue = ""
for (let i = 0; i < cells.length; i++) {
cellContent = cells[i]
for (let j = 0; j < rowCellValues.length; j ++) {
rowCellValue = rowCellValues[j]
if (cellContent.value == rowCellValue) {
// sheet reference passed to the callback
// cell content object passed to the callback
// cell row index passed to the callback
// j+1 <=> cell column index passed to the callback
callback(sheet, cellContent, rowIndex, j+1)
}
}
}
}
/**
* Function:
* - Use parameters to push a new conditional formatting rule to the array of rules
* Do not forget to apply the rules once everything is set
*
* - Parameters:
* - Array of rules
* - Range where the rule will be applied (one range only)
* - Type of rule to use (see acceptable values below)
* - Value to use in the rule
* - Color to use:
* - green
* - yellow
* - orange
* - red
*
* - Return:
* - Array of rules with the new rule pushed in
*/
function createNewConditionalFormatRule(ruleRange, ruleType, ruleValue, ruleColor) {
let rule = SpreadsheetApp.newConditionalFormatRule()
switch (ruleType) {
case 'numberEqualTo':
rule.whenNumberEqualTo(ruleValue)
break
case 'numberNotEqualTo':
rule.whenNumberNotEqualTo(ruleValue)
break
case 'numberGreaterThan':
rule.whenNumberGreaterThan(ruleValue)
break
case 'numberGreaterThanOrEqualTo':
rule.whenNumberGreaterThanOrEqualTo(ruleValue)
break
case 'numberLessThan':
rule.whenNumberLessThan(ruleValue)
break
case 'numberLessThanOrEqualTo':
rule.whenNumberLessThanOrEqualTo(ruleValue)
break
case 'formulaSatisfied':
rule.whenFormulaSatisfied(ruleValue)
break
case 'cellEmpty':
rule.whenCellEmpty()
break
case 'cellNotEmpty':
rule.whenCellNotEmpty()
break
default:
alert("Conditional rule: 'ruleType' error")
return false
}
let greenColor = "#d9ead3"
let yellowColor = "#fff2cc"
let orangeColor = "#fce5cd"
let redColor = "#f4cccc"
switch (ruleColor) {
case 'green':
rule.setBackground(greenColor)
break
case 'yellow':
rule.setBackground(yellowColor)
break
case 'orange':
rule.setBackground(orangeColor)
break
case 'red':
rule.setBackground(redColor)
break
default:
alert("Conditional rule: 'ruleColor' error")
return false
}
rule.setRanges([ruleRange])
rule.build()
return rule
}