-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtility.gs
734 lines (587 loc) · 22 KB
/
Utility.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
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
UtilityClass = function( devMode, errorEmail ) {
devMode = ( devMode || false );
/**
* Copies the data from a range of cells to another range calculated by offset. Both the values and formatting are copied.
* It can be improved for better performance, without using GAS api
* @param {object} sheet the sheet
* @param {string} from the range to return, as specified in A1 notation or R1C1 notation
* @param {number} rowOffset (optional) number of rows down from the range's top-left cell; negative values represent rows up from the range's top-left cell
* @param {number} columnOffset (optional) number of columns right from the range's top-left cell; negative values represent columns left from the range's top-left cell
* @return {object} the new range
* @throws {InvalidArgument}
* @throws {InvalidOffset} If the offset exceed the range
*/
this.A1Offset=function(sheet, a1Range, rowOffset, columnOffset){
rowOffset=(rowOffset || 0);
columnOffset=(columnOffset || 0);
if (!sheet || !a1Range) {
throw "InvalidArgument";
}
fromRange=sheet.getRange(a1Range);
try{
toRange=fromRange.offset(rowOffset, columnOffset);
} catch ( ex ) {
var e=ex;
throw "InvalidOffset";
}
return toRange.getA1Notation();
};
/**
* Copies the data from a range of cells to another range calculated by offset. Both the values and formatting are copied.
* @param {object} sheet the sheet
* @param {string} from the range to return, as specified in A1 notation or R1C1 notation
* @param {number} rowOffset number of rows down from the range's top-left cell; negative values represent rows up from the range's top-left cell * @param {number} columnOffset number of columns right from the range's top-left cell; negative values represent columns left from the range's top-left cell
* @return {object} the new range
*/
this.copyToOffset=function(sheet, from, rowOffset, columnOffset){
fromRange=sheet.getRange(from);
toRange=fromRange.offset(rowOffset, columnOffset);
fromRange.copyTo(toRange);
return toRange;
};
/**
* get the range with only the columns in A1Notation from another range
* @param {string} range the range in A1Notation
* @return {string} the new range in A1Notation
* @throws {InvalidArgument}
*/
this.getRangeColumns=function(range){
var exRange;
if (!range) {
throw "InvalidArgument";
}
exRange=/^([A-Z]+)\d+:([A-Z]+)\d+$/.exec(range);
if(!exRange) {
throw "InvalidRange";
}
return exRange[1]+":"+exRange[2];
};
/**
* converts column number to column letter
* @param {number} number the number of the column (column A is 1)
* @return {string} the letter of the column (column A is 1)
* @deprecated use ConvertA1.indexToColA1
*/
this.numToChar = function( number ) {
return ConvertA1.indexToColA1( number );
};
/**
* show a msgBox
* @param {string} message the message
* @return {bool} false if error
*/
this.popUpAlert = function( message ) {
if ( !message ) {
return false;
}
Browser.msgBox( message );
};
/**
* converts column letter to column number
* @param {string} column letter
* @return {integer} column number
* @deprecated use Convert.colA1ToIndex
*/
this.letterToColumn = function( letter ) {
var column = 0,
length = letter.length;
for ( var i = 0; i < length; i++ ) {
column += ( letter.charCodeAt( i ) - 64 ) * Math.pow( 26, length - i - 1 );
}
return column;
};
/**
* FIND A VALUE INTO A ROW OF A SPECIFIC RANGE
* @param {string} value
* @param {range} range of the row where search
* @return {string} number of column containing the string
*/
this.findValueIntoRow = function( value, range ) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var data = sheet.getRange( range ).getValues();
for ( var i = 0; i < data[ 0 ].length; i++ ) {
if ( data[ 0 ][ i ] == value ) {
return i + 1;
}
}
};
/**
* FIND A VALUE INTO A ROW OF A SPECIFIC RANGE
* @param {string} value
* @param {range} range of the row where search
* @return {ARRAY} RETURN AN ARRAY OF ALL THE OCCURENCY OF THE STRING SEARCHED
*/
this.findValueIntoRowMultipeResult = function( value, range ) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var data = sheet.getRange( range ).getValues();
var res = [];
for ( var i = 0; i < data[ 0 ].length; i++ ) {
if ( data[ 0 ][ i ] == value ) {
res.push( i + 1 );
}
}
return res;
};
/**
* EVALUATE A REGEXP TO ANY COLUMN INTO A ROW OF A SPECIFIC RANGE. IT DOESN'T STOPS ON THE FIRST OCCURANCE
* @param {RegExp} regexp the regexp to be evaluated
* @param {range} range of the row where search
* @return {array} array of number of column containing the string
*/
this.regexEvalIntoRow = function( regexp, range ) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var data = sheet.getRange( range ).getValues();
var result = [];
for ( var i = 0; i < data[ 0 ].length; i++ ) {
if ( regexp.test( data[ 0 ][ i ] ) ) {
result.push( i + 1 );
}
}
return result;
};
/**
* make a toast on the screen
* @param {string} title of toast
* @param {string} text of toast
*/
this.toastInfo = function( text1, text2 ) {
// Show a popup with the title "Status" and the message "Task started".
SpreadsheetApp.getActiveSpreadsheet().toast( text1, text2 );
};
/**
* get GoogleSheetID
* @return {string} GoogleSheetID
*/
this.getGoogleSheetID = function() {
return SpreadsheetApp.getActive().getId(); //current spreadsheet
};
/**
* check if a given string is a valid cell
* @param {string} range the range in A1A1Notation
* @return {bool} true if yes, false otherwise
* @throws {InvalidArgument}
*/
this.isCell=function(range){
if (!range) {
throw "InvalidArgument";
}
return /^[A-Z]+\d+$/.test(range);
};
/**
* check if a given string is a valid range
* @param {string} range the range in A1A1Notation
* @return {bool} true if yes, false otherwise
* @throws {InvalidArgument}
*/
this.isRange=function(range){
if (!range) {
throw "InvalidArgument";
}
return /^([A-Z]+\d+:[A-Z]+\d+|[A-Z]+:[A-Z]+|\d+:\d+)$/.test(range);
};
/**
* check if two given ranges are touched by are touched by each other
* @param {string} rangeA the range eg: F14:G34, B:D
* @param {string} rangeB the range eg: F14:G34, B:D
* @return {bool} true if the cell is in the range, false otherwise
* @throws {InvalidArgument}
*/
this.isRangesOverlap = function( rangeA, rangeB ) {
var ra, rb;
if ( !rangeA || !rangeB ) {
throw "InvalidArgument";
}
ra = ConvertA1.rangeA1ToIndex( rangeA, 1 );
rb = ConvertA1.rangeA1ToIndex( rangeB, 1 );
// If one rectangle is on left side of other
if ( ra.left > rb.right || rb.left > ra.right ) {
return false;
}
// If one rectangle is above other
if ( ra.top > rb.bottom || rb.top > ra.bottom ) {
return false;
}
return true;
};
/**
* check if a given cell is in a range
* @param {string} range the range eg: F14:G34, B:D
* @param {range|string} cell the cell to check or A1Notation for better performance eg. F12
* @return {bool} true if the cell is in the range, false otherwise
*/
this.isInRange = function( range, cell ) {
var editRange, thisRow, thisCol, cellIndex, cellA1;
if ( typeof cell !== "string" ) {
cellA1 = cell.getA1Notation();
} else {
cellA1 = cell;
}
editRange = ConvertA1.rangeA1ToIndex( range, 1 );
cellIndex = ConvertA1.cellA1ToIndex( cellA1, 1 );
// Exit if we're out of range
thisRow = cellIndex.row;
if ( ( editRange.top ) && ( editRange.top ) && ( thisRow < editRange.top || thisRow > editRange.bottom ) ) {
return false;
}
thisCol = cellIndex.col;
if ( thisCol < editRange.left || thisCol > editRange.right ) {
return false;
}
return true;
};
/**
* check if a given cell is in any of a set of ranges
* @param {[string]} ranges array of the range eg: [F14:G34, B:D]
* @param {range|string} cell the cell to check or A1Notation for better performance
* @return {bool} true if the cell is in any range, false otherwise
*/
this.isInAnyRange = function( ranges, cell ) {
var cellA1, r;
if ( !ranges || !cell ) {
return false;
}
if ( typeof cell !== "string" ) {
cellA1 = cell.getA1Notation();
} else {
cellA1 = cell;
}
for ( var i = ranges.length; i--; ) {
r = ranges[ i ];
if ( this.isInRange( r, cellA1 ) ) {
return true;
}
}
return false;
};
/**
* get the acitve cell value (useful for sidebar and dialog)
* @return {object} the value in the cell
*/
this.getActiveCellValue = function() {
return SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getActiveCell().getValue();
};
/**
* workaround that allows you to call any library function if you paste in this one generic wrapper function. Then you can call this from the spreadsheet.
* For example, if I had a library called MyLib with a function add(x, y) (pretend x is in cell A1 and y is in cell A2) I could call it like this: =LIB_FUNC("MyLib", "add", A1, A2).
* @param {string} functionName
* @constructor
*/
this.LIB_FUNC = function( functionName ) {
var currFn = this;
var extraArgs = [];
var fnArr = functionName.split( "." );
var fnArr_length = fnArr.length;
for ( var i = 0; i < fnArr_length; i++ ) {
currFn = currFn[ fnArr[ i ] ];
if ( !currFn ) throw "No such function: " + fnArr[ i ];
}
if ( arguments.length > 1 ) {
extraArgs = Array.apply( null, arguments ).slice( 1 );
}
return currFn.apply( this, extraArgs );
};
/**
* sets the value of the current cell
* @param {string} value the value to set
*/
this.setCellValue = function( range, value ) {
var cell = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange( range );
if ( !cell ) return;
cell.setValue( value );
};
/**
* includes html files into an html
* @param {string} filename
* @return {string} the content
*/
this.include = function( filename ) {
return HtmlService.createTemplateFromFile( filename ).evaluate().getContent();
};
/**
* sends a debug email message
* @param {string} message debug info to send
* @param {string} to (optional)email to send the error, if not defined errorEmail will taken
*/
this.sendErrorEmails = function( message, to ) {
to = ( to || errorEmail );
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var activeRange = sheet.getActiveRange();
if ( devMode || !to ) {
return;
}
try {
throw new Error();
} catch ( ex ) {
var title = "Error message in spreadsheet: " + ss.getName();
body = "<b>Spreadsheet</b>: <a href='" + ss.getUrl() + "'>" + ss.getName() + "</a><br>" +
"<b>Sheet</b>:" + sheet.getName() + "<br>" +
"<b>Error message</b>: " + message + "<br>" +
"<b>Current range</b>: " + activeRange.getA1Notation() + "<br>" +
"<b>Stacktrace</b>: " + ex.stack;
MailApp.sendEmail( {
to: to,
subject: title,
htmlBody: body
} );
}
};
/**
* Returns a two-dimensional array of values, indexed by row, then by column. Same as Range.getValues() but faster and works on an array rappresenting the sheet data.
* @param {array} sheetValues all the data in the sheet. from first column to the last
* @param {string} range range in A1Notation
* @return {array} a two-dimensional array of values, indexed by row, then by column
*/
this.getRangeValuesFromArray = function( sheetValues, range ) {
var rangeIndexes, rangeVals, row, rows, _i, _len;
rangeVals = [];
rangeIndexes = ConvertA1.rangeA1ToIndex( range );
rows = sheetValues.slice( rangeIndexes.top, rangeIndexes.bottom + 1 );
for ( _i = 0, _len = rows.length; _i < _len; _i++ ) {
row = rows[ _i ];
rangeVals.push( row.slice( rangeIndexes.left, rangeIndexes.right + 1 ) );
}
return rangeVals;
};
/**
* Returns the value of a cell. Same as Range.getValue() but faster and works on an array rappresenting the sheet data.
* @param {array} sheetValues all the data in the sheet. from first column to the last
* @param {string} cellA1 cell in A1Notation
* @return {string} the value
*/
this.getCellValueFromArray = function( sheetValues, cellA1 ) {
var cellIndexes;
cellIndexes = ConvertA1.cellA1ToIndex( cellA1 );
return sheetValues[ cellIndexes.row ][ cellIndexes.col ];
};
/**
* Executes a provided function once for each sheet of a spreadsheet.
* @param {string} sourceId (optional) the source id, if not defined will be taken the current spreadsheet
* @param {RegExp} regexSheetName regular expression to copy only mathcing sheets name. eg /^Template_.+/
* @param {Function} callback Function to execute for each element, taking 2 arguments: sheet, sheetName
* @throws {SheetNotFound} if no sheet is found
*/
this.forEachSheet = function( sourceId, regexSheetName, callback ) {
var source, sourceSheets, _sheetName, _i, _j, _len, _sheet;
regexSheetName = ( regexSheetName || /.*/ );
if ( !callback ) {
return false;
}
try {
source = ( sourceId ) ? SpreadsheetApp.openById( sourceId ) : SpreadsheetApp.getActiveSpreadsheet();
} catch ( e ) {
throw "SheetNotFound";
}
sourceSheets = source.getSheets();
for ( _i = 0, _len = sourceSheets.length; _i < _len; _i++ ) {
_sheet = sourceSheets[ _i ];
_sheetName = _sheet.getName();
if ( regexSheetName.test( _sheetName ) ) {
callback( _sheet, _sheetName );
}
}
};
/**
* execute a callback for each row in a sheet
* @param {objecy} sheet the sheet
* @param {Function} callback Function to execute for each element, taking 2 arguments: sheet, row number
* @return {void}
* @throws {InvalidArgument}
*/
this.forEachRow = function( sheet, callback ) {
if ( !sheet || !callback ) {
throw "InvalidArgument";
}
for ( var i = 1, rows_length = sheet.getLastRow(); i <= rows_length; i++ ) {
callback( sheet, i );
}
};
/**
* execute a callback for each column in a sheet
* @param {objecy} sheet the sheet
* @param {Function} callback Function to execute for each element, taking 2 arguments: sheet, column number
* @return {void}
* @throws {InvalidArgument}
*/
this.forEachColumn = function( sheet, callback ) {
if ( !sheet || !callback ) {
throw "InvalidArgument";
}
for ( var i = 1, cols_length = sheet.getLastColumn(); i <= cols_length; i++ ) {
callback( sheet, i );
}
};
/**
* delete all mathcing sheet
* @return {void}
* @param {RegExp} regexSheetName regular expression to copy only mathcing sheets name. eg /^Template_.+/
* @throws {InvalidArgument}
*/
this.deleteMatchingSheets = function( regexSheetName ) {
var spreadsheet;
if ( !regexSheetName ) {
throw "InvalidArgument";
}
spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
this.forEachSheet( null, regexSheetName, function( sheet, sheetName ) {
spreadsheet.deleteSheet( sheet );
} );
};
/**
* copy all sheets from a Spreadsheet to another. !!IMPORTANT!! has to be completed
* @param {string} sourceId the source id
* @param {RegExp} regexSheetName regular expression to copy only mathcing sheets name. eg /^Template_.+/
* @return {void}
* @throws {InvalidArgument}
*/
this.copyAllSheetsHere = function( sourceId, regexSheetName ) {
var dest, destSheets;
regexSheetName = ( regexSheetName || /.*/ );
dest = SpreadsheetApp.getActiveSpreadsheet();
destSheets = dest.getSheets();
if ( !sourceId ) {
throw "InvalidArgument";
}
this.forEachSheet( sourceId, regexSheetName, function( sheet, sheetName ) {
dest.deleteSheet( dest.getSheetByName( sheetName ) );
sheet.copyTo( dest ).setName( sheetName );
} );
};
/**
* Interpolace string with keys with the object
* with key values passed
*
* @param {string} text string to be interpolated
* @param {object} keyValue object with key values to subtitute on the string
* @param {RegExp} [delimiter=/{{([^{}]*)}}/g] regexp that defines the delimiter, default is {{word}}
*
* @returns {string} The interpolated string
*
* @example
*
* var interpolated = interpolate('Hello {{name}}, it is me {{daemon}}.', {
* name: 'Lyra',
* daemon: 'pantalaimon'
* });
*
* console.log(interpolated === 'Hello Lyra, it is me pantalaimon.')
*/
this.interpolate = function( text, keyValues, delimiter ) {
delimiter = delimiter || /{{([^{}]*)}}/g;
return text.replace(
delimiter,
function( matched, key ) {
var value = keyValues[ key ];
return typeof value === 'string' || typeof value === 'number' ? value : matched;
}
);
};
/**
* Make a string's first character uppercase
* @param {string} str
* @return {string}
*/
this.ucfirst = function( str ) {
return str.charAt( 0 ).toUpperCase() + str.slice( 1 );
};
/**
* execute a callback for each named range in a spreadSheet
* @param {object} spreadSheet the spreadSheet
* @param {Function} callback callback to run. Taking 1 argument: the named range
* @return {void}
* @throws "InvalidArgument"
*/
this.forEachNamedRange = function( spreadSheet, callback ) {
var nRanges;
spreadSheet = spreadSheet || SpreadSheetCache.getActiveSpreadsheet();
if ( !spreadSheet ) {
throw "InvalidArgument";
}
nRanges = spreadSheet.getNamedRanges();
for ( var i = 0, nRanges_length = nRanges.length, e; i < nRanges_length; i++ ) {
e = nRanges[ i ];
callback( e );
}
};
/**
* delete all namedRange in a spreadSheet
* @param {object} spreadSheet the spreadSheet
* @return {void}
* @throws "InvalidArgument"
*/
this.deleteAllNamedRanges = function( spreadSheet ) {
if ( !spreadSheet ) {
throw "InvalidArgument";
}
this.forEachNamedRange( spreadSheet, function( r ) {
r.remove();
} );
};
/**
* copy all namedRange from a spreadSheet to another one
* @param {object} dest destination spreadSheet
* @param {object} source source spreadSheet
* @return {void}
* @throws "InvalidArgument"
*/
this.copyAllNamedRanges = function( dest, source ) {
source = source || SpreadSheetCache.getActiveSpreadsheet();
if ( !dest ) {
throw "InvalidArgument";
}
this.forEachNamedRange( source, function( r ) {
var rName, a1n, sheetName, destRange;
rName = r.getName();
sheetName = r.getRange().getSheet().getName();
a1n = r.getRange().getA1Notation();
destRange = dest.getRange( sheetName + "!" + a1n );
dest.setNamedRange( rName, destRange );
} );
};
/**
* copy all spreadsheet values from a spreadsheet to another
* @param {string} from the spreadsheet id where values are
* @param {string} to the spreadsheet id where to write values
* @param {RegExp} sheetNameFilter (optional) regex to choose what sheet copy
* @return {void}
* @throws "InvalidArgument"
* @throws "SheetNotFound" if the sheet in the destination spreadsheet is not found
*/
this.copyAllSpreadSheetValues = function( from, to, sheetNameFilter ) {
sheetNameFilter = ( sheetNameFilter || /.*/ );
var toSpreadSheet;
if ( !from || !to ) {
throw "InvalidArgument";
}
toSpreadSheet = SpreadsheetApp.openById( to );
Utility.forEachSheet( from, sheetNameFilter, function( fromSheet, sheetName ) {
var toSheet, fromValues;
toSheet = toSpreadSheet.getSheetByName( sheetName );
if ( !toSheet ) {
throw "SheetNotFound";
}
fromValues = fromSheet.getDataRange().getValues();
//write the data to the destination
toSheet.getDataRange().setValues( fromValues );
} );
};
/**
* Returns the position of the last column that has content.
* @param {string} sheet (optional) the sheet name
* @return {number} the last column of the sheet that contains content
* @throws {InvalidArgument}
* @throws {SheetNotFound}
*/
this.getLastColumn = function( sheet ) {
if ( !sheet ) {
throw "InvalidArgument";
} else if ( typeof sheet === 'string' ) {
sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName( sheet );
if ( !sheet ) {
throw "SheetNotFound";
}
return sheet.getLastColumn();
}
};
};
Utility = new UtilityClass();