1+ 'use strict' ;
2+ /*global SpreadsheetApp: false */
3+ function HeaderRow ( spreadsheet , sheetName , headerRowNumber , startColumnNumber , columnTitles , overwritePrevious ) {
4+ if ( arguments . length !== 6 ) {
5+ throw { 'name' : 'Error' ,
6+ 'message' : '"HeaderRow()" constructor function requires 6 arguments!' } ;
7+ }
8+ this . spreadsheet = spreadsheet ;
9+ this . sheetName = sheetName ;
10+ this . headerRowNumber = headerRowNumber ;
11+ this . startColumnNumber = startColumnNumber ;
12+ this . columnTitles = columnTitles ;
13+ this . overwritePrevious = overwritePrevious ;
14+ this . sheet = this . spreadsheet . getSheetByName ( this . sheetName ) ;
15+ this . columnTitleCount = this . columnTitles . length ;
16+ this . headerRowRange = this . sheet . getRange ( this . headerRowNumber ,
17+ this . startColumnNumber ,
18+ 1 ,
19+ this . columnTitleCount ) ;
20+ this . headerRowRange . setFontWeight ( 'normal' ) ;
21+ this . headerRowRange . setFontStyle ( 'normal' ) ;
22+ this . addColumnTitlesToHeaderRow ( ) ;
23+ }
24+
25+ HeaderRow . prototype = {
26+ constructor : HeaderRow ,
27+ freezeHeaderRow : function ( ) {
28+ var sheet = this . sheet ;
29+ sheet . setFrozenRows ( this . headerRowNumber ) ;
30+ } ,
31+ setHeaderFontWeightBold : function ( ) {
32+ this . headerRowRange . setFontWeight ( 'bold' ) ;
33+ } ,
34+ setFontStyle : function ( style ) {
35+ this . headerRowRange . setFontStyle ( style ) ;
36+ } ,
37+ addCommentToColumn : function ( comment , headerRowColumnNumber ) {
38+ var cellToComment = this . headerRowRange . getCell ( 1 , headerRowColumnNumber ) ;
39+ cellToComment . setComment ( comment ) ;
40+ } ,
41+ addColumnTitlesToHeaderRow : function ( ) {
42+ var i ,
43+ titleCell ;
44+ this . spreadsheet . setNamedRange ( this . headerRowRangeName , this . headerRowRange ) ;
45+ for ( i = 1 ; i <= this . columnTitleCount ; i += 1 ) {
46+ titleCell = this . headerRowRange . getCell ( 1 , i ) ;
47+ if ( titleCell . getValue ( ) && ! this . overwritePrevious ) {
48+ throw { 'name' : 'Error' ,
49+ 'message' : '"HeaderRow.addColumnTitlesToHeaderRow()" Cannot overwrite previous values!' } ;
50+ }
51+ titleCell . setValue ( this . columnTitles [ i - 1 ] ) ;
52+ }
53+ } ,
54+ setHeaderRowName : function ( rngName ) {
55+ this . spreadsheet . setNamedRange ( rngName , this . headerRowRange ) ;
56+ }
57+ } ;
58+
59+ function test_HeaderRow ( ) {
60+ var ss = SpreadsheetApp . getActiveSpreadsheet ( ) ,
61+ sheetName = ss . getActiveSheet ( ) . getSheetName ( ) ,
62+ headerRowNumber = 3 ,
63+ startColumnNumber = 2 ,
64+ columnTitles = [ 'col1' , 'col2' , 'col3' ] ,
65+ hr = new HeaderRow ( ss , sheetName , headerRowNumber , startColumnNumber , columnTitles , true ) ;
66+ hr . freezeHeaderRow ( ) ;
67+ hr . setHeaderFontWeightBold ( ) ;
68+ hr . setFontStyle ( 'oblique' ) ;
69+ hr . addCommentToColumn ( 'Comment added ' + Date ( ) , 2 ) ;
70+ hr . setHeaderRowName ( 'header' ) ;
71+ }
0 commit comments