'use strict' ; import type DataEventEmitter from '../DataEventEmitter' ; import type DataValueType from '../DataValueType' ; import DataParser from './DataParser.js' ; import DataTable from '../DataTable.js' ; import DataConverter from '../DataConverter.js' ; import U from '../../Core/Utilities.js' ; const { merge , uniqueKey } = U ; private class GoogleSheetsParser extends DataParser < DataParser . Event > { /**
     * Default options
     */ protected static readonly defaultOptions : ( GoogleSheetsParser . ClassJSONOptions ) = { ... DataParser . defaultOptions , json : { } } ; param { GoogleSheetsParser . OptionsType } options param { DataConverter } converter constructor ( options ? , converter ? ) { super ( ) ; this . columns = [ ] ; this . headers = [ ] ; this . options = merge ( GoogleSheetsParser . defaultOptions , options ) ; this . converter = converter || new DataConverter ( ) ; } private columns : DataTable . CellType [ ] [ ] ; private headers : string [ ] ; public converter : DataConverter ; public options : GoogleSheetsParser . ClassJSONOptions ; private getSheetColumns ( json ) { const parser = this , { startColumn , endColumn , startRow , endRow } = parser . options , columns = [ ] , cells = json . feed . entry , cellCount = ( cells || [ ] ) . length ; let cell , colCount = 0 , rowCount = 0 , val , gr , gc , cellInner , i , j ; for ( i = 0 ; i < cellCount ; i ++ ) { cell = cells [ i ] ; colCount = Math . max ( colCount , cell . gs$cell . col ) ; rowCount = Math . max ( rowCount , cell . gs$cell . row ) ; } for ( i = 0 ; i < colCount ; i ++ ) { if ( i >= startColumn && i <= endColumn ) { columns [ i - startColumn ] = [ ] ; } } for ( i = 0 ; i < cellCount ; i ++ ) { cell = cells [ i ] ; gr = cell . gs$cell . row - 1 ; gc = cell . gs$cell . col - 1 ; if ( gc >= startColumn && gc <= endColumn && gr >= startRow && gr <= endRow ) { cellInner = cell . gs$cell || cell . content ; val = null ; if ( cellInner . numericValue ) { if ( cellInner . $t . indexOf ( '/' ) >= 0 || ( cellInner . $t . indexOf ( '-' ) >= 0 && cellInner . $t . indexOf ( '.' ) === - 1 ) ) { val = cellInner . $t ; } else if ( cellInner . $t . indexOf ( '%' ) > 0 ) { val = parseFloat ( cellInner . numericValue ) * 100 ; } else { val = parseFloat ( cellInner . numericValue ) ; } } else if ( cellInner . $t && cellInner . $t . length ) { val = cellInner . $t ; } columns [ gc - startColumn ] [ gr - startRow ] = val ; } } for ( i = 0 ; i < colCount ; i ++ ) { const column = columns [ i ] ; if ( column . length ) { for ( i = 0 ; i < column . length ; i ++ ) { if ( typeof column [ i ] === 'undefined' ) { column [ i ] = null as any ; } } } } return columns ; } param { GoogleSheetsParser . OptionsType } options param { DataEventEmitter . EventDetail } eventDetail emits emits public parse ( jsonProp , eventDetail ? ) { const parser = this , parserOptions = merge ( true , parser . options , { json : jsonProp } ) , converter = parser . converter , json = parserOptions . json , cells = json . feed . entry , headers = parser . headers ; let column ; if ( ! cells || cells . length === 0 ) { return false ; } parser . headers = [ ] ; parser . columns = [ ] ; parser . emit < DataParser . Event > ( { type : 'parse' , columns : parser . columns , detail : eventDetail , headers : parser . headers } ) ; parser . columns = parser . getSheetColumns ( json ) ; for ( let i = 0 , iEnd = parser . columns . length ; i < iEnd ; i ++ ) { column = parser . columns [ i ] ; parser . headers [ i ] = parserOptions . firstRowAsNames ? column . splice ( 0 , 1 ) . toString ( ) : uniqueKey ( ) ; for ( let j = 0 , jEnd = column . length ; j < jEnd ; ++ j ) { if ( column [ j ] && typeof column [ j ] === 'string' ) { let cellValue = converter . asGuessedType ( column [ j ] as string ) ; if ( cellValue instanceof Date ) { cellValue = cellValue . getTime ( ) ; } parser . columns [ i ] [ j ] = cellValue ; } } } parser . emit < DataParser . Event > ( { type : 'afterParse' , columns : parser . columns , detail : eventDetail , headers : parser . headers } ) ; } return { DataTable } public getTable ( ) { return DataParser . getTableFromColumns ( this . columns , this . headers ) ; } } namespace GoogleSheetsParser { /**
     * The available options for the parser
     */ export type OptionsType = Partial < ClassJSONOptions > ; /**
     * Options for the parser compatible with ClassJSON
     */ export interface ClassJSONOptions extends DataParser . Options { json : Highcharts . JSONType ; } } export default GoogleSheetsParser ;