-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpopulate_sheets.R
474 lines (449 loc) · 18.7 KB
/
populate_sheets.R
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
PopulateSheets <- function(workbook,
current.translation,
latest.translation,
language.file.suffix, # e.g. 'chi' for chinese translation file
sheet.names,
column.version.current = 'Current',
column.version.latest = 'Latest',
start.at.row = 1) {
source('constants.R')
source('sort_sheet_columns.R')
sheets <- readWorksheet(workbook, sheet.names)
#################### initialisations, variables, constants, and function definitions ####################
# define cell style and color for Excel for highlighting
## cell styles for translation sheets
### define cell style and color for header row
kCellStyleHeader <- createCellStyle(workbook)
setFillPattern(kCellStyleHeader,
fill = XLC$FILL.SOLID_FOREGROUND)
setFillForegroundColor(kCellStyleHeader,
color = XLC$COLOR.LIGHT_BLUE)
### define cell style and color for row error
kCellStyleRowError <- createCellStyle(workbook)
setWrapText(kCellStyleRowError,
wrap = T)
setFillPattern(kCellStyleRowError,
fill = XLC$FILL.SOLID_FOREGROUND)
setFillForegroundColor(kCellStyleRowError,
color = XLC$COLOR.ROSE)
### define cell style and color for row changes
kCellStyleRowChanged <- createCellStyle(workbook)
setWrapText(kCellStyleRowChanged,
wrap = T)
setFillPattern(kCellStyleRowChanged,
fill = XLC$FILL.SOLID_FOREGROUND)
setFillForegroundColor(kCellStyleRowChanged,
color = XLC$COLOR.LIGHT_YELLOW)
### define cell style and color for cell error
kCellStyleCellError <- createCellStyle(workbook)
setWrapText(kCellStyleCellError,
wrap = T)
setFillPattern(kCellStyleCellError,
fill = XLC$FILL.SOLID_FOREGROUND)
setFillForegroundColor(kCellStyleCellError,
color = XLC$COLOR.RED)
### define cell style and color for cell changes
kCellStyleCellChanged <- createCellStyle(workbook)
setWrapText(kCellStyleCellChanged,
wrap = T)
setFillPattern(kCellStyleCellChanged,
fill = XLC$FILL.SOLID_FOREGROUND)
setFillForegroundColor(kCellStyleCellChanged,
color = XLC$COLOR.LIGHT_ORANGE)
### define cell style for wrapping text
kCellStyleWrapText <- createCellStyle(workbook)
setWrapText(kCellStyleWrapText, wrap = T)
## cell styles for summary sheets
### cell style for row OK
kCellStyleSummaryOk <- createCellStyle(workbook)
setFillPattern(kCellStyleSummaryOk,
fill = XLC$FILL.SOLID_FOREGROUND)
setFillForegroundColor(kCellStyleSummaryOk,
color = XLC$COLOR.LIGHT_GREEN)
### cell style for row CHANGED (Old/New keys)
kCellStyleSummaryChanged <- createCellStyle(workbook)
setFillPattern(kCellStyleSummaryChanged,
fill = XLC$FILL.SOLID_FOREGROUND)
setFillForegroundColor(kCellStyleSummaryChanged,
color = XLC$COLOR.YELLOW)
### cell style for row details CHANGED (Old/New keys)
kCellStyleSummaryChangedDetails <- createCellStyle(workbook)
setFillPattern(kCellStyleSummaryChangedDetails,
fill = XLC$FILL.SOLID_FOREGROUND)
setFillForegroundColor(kCellStyleSummaryChangedDetails,
color = XLC$COLOR.LIGHT_YELLOW)
### cell style for row ERROR
kCellStyleSummaryError <- createCellStyle(workbook)
setFillPattern(kCellStyleSummaryError,
fill = XLC$FILL.SOLID_FOREGROUND)
setFillForegroundColor(kCellStyleSummaryError,
color = XLC$COLOR.RED)
### cell style for row details ERROR
kCellStyleSummaryErrorDetails <- createCellStyle(workbook)
setFillPattern(kCellStyleSummaryErrorDetails,
fill = XLC$FILL.SOLID_FOREGROUND)
setFillForegroundColor(kCellStyleSummaryErrorDetails,
color = XLC$COLOR.LIGHT_ORANGE)
# summary sheet
kSummarySheetName <- 'Summary Sheets'
kSummaryColumnNameSheet <- 'Sheet'
kSummaryColumnNameStatus <- 'Status'
kSummaryColumnNameDescription <- 'Description'
kSummaryColumnLength <- 3
# indicates position for next summary
summary.row.index <- 1
# the summary to be filled into the sheet
summary <- data.frame(
Sheet = character(),
Status = character(),
stringsAsFactors = FALSE
)
# create sheet only when it does not exists yet
if (!kSummarySheetName %in% sheet.names) {
createSheet(workbook, name = kSummarySheetName)
}
# the lists containing row indices with status ok or error
summary.row.list.sheet.status.ok <- list()
summary.row.list.sheet.status.oldnew <- list()
summary.row.list.sheet.details.oldnew <- list()
summary.row.list.sheet.status.error <- list()
summary.row.list.sheet.details.error <- list()
#################### END ####################
# process for each sheet:
# read key in each row
# based on the key read the translation data frames and populate cells
for (sheet.name in sheet.names) {
current.sheet <- readWorksheet(workbook, sheet = sheet.name)
current.sheet <- SortSheetColumns(current.sheet, language.file.suffix, column.version.current, column.version.latest)
# initialize cell style list holding changed and error rows
cell.style.rows.df <-
read.table(text=paste(Translation$Internal.Style.Columns.All, collapse=' '),
header=T)
# summary of processing
overall.status <- Translation$Xls.Sheet.Summary.StatusCode.Ok
# write sheet name in summary column 'Sheet'
summary[summary.row.index, kSummaryColumnNameSheet] <- sheet.name
# row position of sheet name in summary
summary.sheet.name.index <- summary.row.index
# all other infos goes to next line
summary.row.index <- summary.row.index + 1
xls.text.columns <- list(Translation$Xml.File.Suffix.Main1,
Translation$Xml.File.Suffix.Main2,
language.file.suffix)
# is there any data?
row.numbers <- nrow(current.sheet)
if (row.numbers >= start.at.row) {
print(paste('Populating sheet', sheet.name))
# iterate through each row and start filling the sheet
# using our translation data frames
for (row.number in start.at.row:row.numbers) {
#print(c('Process row', row.number+1))
# read key from sheet
cell.value <- current.sheet[row.number, Translation$Xml.Attribute.Key]
# skip if Keycell is NULL or NA
if (is.null(cell.value) || is.na(cell.value)) {
next
}
# get translation for this key
current.translation.row <-
current.translation[current.translation$Key == cell.value,]
latest.translation.row <-
latest.translation[latest.translation$Key == cell.value,]
# there must be exactly one translation
current.nrow <- nrow(current.translation.row)
latest.nrow <- nrow(latest.translation.row)
# flag to make sure it appears only once in the summary sheet
# and not 3 times because of 3 text columns
oldnew.summary <- FALSE
if (current.nrow == 1 || latest.nrow == 1) {
# set ID in sheet
current.sheet[row.number, 'ID'] <- if (current.nrow == 1) {
current.translation.row$ID
} else {
latest.translation.row$ID
}
# fill columns
for (column.name in xls.text.columns) {
# set cell value for current translation
current.sheet[row.number, paste(column.name, column.version.current)] <-
if (current.nrow == 1) {
current.translation.row[column.name]
} else {
paste('[NEW key added in release ', column.version.latest, ', but not existent in', column.version.current, '!]')
}
# set cell value for latest translation
current.sheet[row.number, paste(column.name, column.version.latest)] <-
if (latest.nrow == 1) {
latest.translation.row[column.name]
} else {
paste('[OLD key removed in release ', column.version.latest, ', but existent in', column.version.current, '!]')
}
# key exists only in current or latest?
key.in.current.latest <- current.nrow == 1 & latest.nrow == 1
if (!oldnew.summary && !key.in.current.latest) {
oldnew.summary <- TRUE
# fill text in column status
summary[summary.row.index, kSummaryColumnNameStatus] <-
paste('Old/New key \'',
cell.value, '\'',
sep = '')
# fill text in column description
summary[summary.row.index, kSummaryColumnNameDescription] <-
if (current.nrow != 1) {
paste('NEW key added in release ',
column.version.latest,
', but not existent in',
column.version.current,
', sheet row', row.number+1)
} else {
paste('OLD key in release ',
column.version.latest,
', but removed in',
column.version.current,
', sheet row', row.number+1)
}
# store row position of old/new key details in list
summary.row.list.sheet.details.oldnew <-
c(summary.row.list.sheet.details.oldnew, summary.row.index + 1)
summary.row.index <- summary.row.index + 1
}
# cell style in case:
## - key does not exist either in current or latest version
## - text is different between current and latest
if (!key.in.current.latest || # no key
latest.translation.row[column.name] != # text differences
current.translation.row[column.name]) {
#if (!key.in.current.latest) print(paste('> old/new key ', column.name, ' in row ', row.number))
#print(paste('> change ', column.name, ' in row ', row.number, ' \'', current.translation.row[column.name], '\' to \'', latest.translation.row[column.name], '\'', sep = ''))
# store style for changed cell
cell.style.rows.df[nrow(cell.style.rows.df) + 1, ] <-
c(column.name, row.number + 1, Translation$Internal.Style.Df.Style.Type.Cell)
}
}
} else {
overall.status <- Translation$Xls.Sheet.Summary.StatusCode.Error
# fill text in column status
summary[summary.row.index, kSummaryColumnNameStatus] <-
paste('Unknown key \'',
cell.value, '\'',
sep = '')
# store row position of error details in list
summary.row.list.sheet.details.error <-
c(summary.row.list.sheet.details.error, summary.row.index + 1)
# fill text in column description
summary[summary.row.index, kSummaryColumnNameDescription] <-
paste(current.nrow, ' current and ', latest.nrow,
' latest translations found in sheet, row ',
row.number+1,
'.',
sep = '')
summary.row.index <- summary.row.index + 1
# store style for row error
cell.style.rows.df[nrow(cell.style.rows.df) + 1, ] <-
# add any value (e.g. key) in first column and not an empty ''
# otherwise NA is added and an error occurs
c(Translation$Xml.Attribute.Key, row.number + 1, Translation$Internal.Style.Df.Style.Type.Row)
}
}
print('> update worksheet')
# first write the sheet back into the workbook
# before doing further changes e.g. cell styles
writeWorksheet(workbook, current.sheet, sheet = sheet.name)
# TODO: data frame elements are for some reason a list with a single value
# here we need to transform into new data frame by
# extracting each value and defining type (as.numeric, etc.)
cell.style.rows.df <- transform(cell.style.rows.df,
column.name = as.character(column.name),
row.number = as.numeric(row.number),
style.type = as.numeric(style.type))
print('> update styles in sheet')
# update sheet styles
# something has changed?
# highlight the rows with our defined cell styles above
for (row.number in start.at.row: (row.numbers + 1)) {
# row has extra style?
# nb: there can be more rows for a row number since
# several columns can be changed
# first check if there are rows (nrow) because
# calling which on empty causes an error
style.row.indices <- if (nrow(cell.style.rows.df) == 0) {
integer(0)
} else {
which(row.number == cell.style.rows.df[Translation$Internal.Style.Df.Row.Number])
}
if (length(style.row.indices) > 0) {
# cell style for complete row has changed
setCellStyle(workbook,
sheet = sheet.name,
row = row.number,
col = 1:length(colnames(current.sheet)),
cellstyle = kCellStyleRowChanged)
# set styles for some cells this row
for (style.row.index in style.row.indices) {
# get row style for each cell
style.row <- cell.style.rows.df[style.row.index,]
if (style.row[Translation$Internal.Style.Df.Style.Type] == Translation$Internal.Style.Df.Style.Type.Cell) {
#print(paste('> cell style for column', paste(style.row[[Translation$Internal.Style.Df.Column.Name]],column.version.current), ' in row', row.number))
# cell style for current translation release column
setCellStyle(workbook,
sheet = sheet.name,
row = row.number,
col = which(colnames(current.sheet) ==
paste(style.row[[Translation$Internal.Style.Df.Column.Name]],
column.version.current)),
cellstyle = kCellStyleCellChanged)
#print(paste('> cell style for column', paste(style.row[[Translation$Internal.Style.Df.Column.Name]],column.version.latest), ' in row', row.number))
# cell style for latest translation release column
setCellStyle(workbook,
sheet = sheet.name,
row = row.number,
col = which(colnames(current.sheet) ==
paste(style.row[[Translation$Internal.Style.Df.Column.Name]],
column.version.latest)),
cellstyle = kCellStyleCellChanged)
} else {
# cell style for complete row error
setCellStyle(workbook,
sheet = sheet.name,
row = style.row[[Translation$Internal.Style.Df.Row.Number]],
col = 1:length(colnames(current.sheet)),
cellstyle = kCellStyleRowError)
# cell style for error in Key column
setCellStyle(workbook,
sheet = sheet.name,
row = style.row[[Translation$Internal.Style.Df.Row.Number]],
col = which(colnames(current.sheet) ==
style.row[[Translation$Internal.Style.Df.Column.Name]]),
cellstyle = kCellStyleCellError)
#stop for loop since complete row is marked
break;
}
}
} else {
# ATTENTION: this slows down the perfomance!!!
# cell style for wrapping text on complete sheet
setCellStyle(workbook,
sheet = sheet.name,
row = row.number,
col = 1:length(colnames(current.sheet)),
cellstyle = kCellStyleWrapText)
}
}
print('> done populating sheet')
} else {
# save unchanged worksheet anyways since we have re-ordered columns
writeWorksheet(workbook, current.sheet, sheet = sheet.name)
}
# write output for number of sheet changes in each column
total.changes <- 0
summary.column.status.text <- ''
for (column.name in xls.text.columns) {
# first check if there are rows (nrow) because
# calling which on empty causes an error
changes <- if (nrow(cell.style.rows.df) == 0) {
0
} else {
length(which(cell.style.rows.df[Translation$Internal.Style.Df.Column.Name] == column.name))
}
total.changes <- total.changes + changes
summary.column.status.text <- paste(summary.column.status.text,
' ', changes,
' change(s) in ',
column.name, '.',
sep = '')
}
print(paste('>',summary.column.status.text))
summary[summary.row.index, kSummaryColumnNameDescription] <-
summary.column.status.text
summary[summary.row.index, kSummaryColumnNameStatus] <-
paste(total.changes,
' total changes.')
# set header cell style for each sheet
setCellStyle(workbook,
sheet = sheet.name,
row = 1,
col = 1:length(colnames(current.sheet)),
cellstyle = kCellStyleHeader)
summary.row.index <- summary.row.index + 1
if (overall.status == Translation$Xls.Sheet.Summary.StatusCode.Ok) {
summary.row.list.sheet.status.ok <-
c(summary.row.list.sheet.status.ok,
summary.sheet.name.index + 1)
} else {
summary.row.list.sheet.status.error <-
c(summary.row.list.sheet.status.error,
summary.sheet.name.index + 1)
}
summary[summary.sheet.name.index, kSummaryColumnNameStatus] <- overall.status
# set column widths
# 1st column description width is 10 characters long
setColumnWidth(workbook,
sheet = sheet.name,
column =
which(colnames(current.sheet) == Translation$Xls.Column.Other.Description),
width = 10 * 256)
for (i in 2:length(colnames(current.sheet))) {
# auto-size for id and key column
# width 20 chars long for all other columns
columnWidth <-
if (i == which(colnames(current.sheet) == Translation$Xml.Attribute.Id)) {
10 * 256
} else if (i == which(colnames(current.sheet) == Translation$Xml.Attribute.Key)) {
15 * 256
} else {
20 * 256
}
setColumnWidth(workbook,
sheet = sheet.name,
column = i,
width = columnWidth)
}
} # end of processing sheets in for-loop
# first write data before updating style sheet
writeWorksheet(workbook,
summary,
sheet = kSummarySheetName)
setCellStyle(workbook,
sheet = kSummarySheetName,
row = 1,
col = 1:kSummaryColumnLength,
cellstyle = kCellStyleHeader)
for (i in 2:nrow(summary)) {
# set style for status ok or error
if (i %in% summary.row.list.sheet.status.ok) {
setCellStyle(workbook,
sheet = kSummarySheetName,
row = i,
col = 1:kSummaryColumnLength,
cellstyle = kCellStyleSummaryOk)
} else if (i %in% summary.row.list.sheet.status.oldnew) {
setCellStyle(workbook,
sheet = kSummarySheetName,
row = i,
col = 1:kSummaryColumnLength,
cellstyle = kCellStyleSummaryChanged)
} else if (i %in% summary.row.list.sheet.details.oldnew) {
setCellStyle(workbook,
sheet = kSummarySheetName,
row = i,
col = 2:kSummaryColumnLength,
cellstyle = kCellStyleSummaryChangedDetails)
} else if (i %in% summary.row.list.sheet.status.error) {
setCellStyle(workbook,
sheet = kSummarySheetName,
row = i,
col = 1:kSummaryColumnLength,
cellstyle = kCellStyleSummaryError)
} else if (i %in% summary.row.list.sheet.details.error) {
setCellStyle(workbook,
sheet = kSummarySheetName,
row = i,
col = 2:kSummaryColumnLength,
cellstyle = kCellStyleSummaryErrorDetails)
}
}
setColumnWidth(workbook, sheet = kSummarySheetName, column = 1:3, width = -1)
# sets the active sheet
# (though the tab itself is not focused and highlighted...)
setActiveSheet(workbook, sheet = kSummarySheetName)
}