<commit_before>Promise = require('bluebird')
{parseStringAsync} = Promise.promisifyAll(require('xml2js'))
processors = require('xml2js/lib/processors')
Row = require('./row')
Validation = require('./validation')
Utils = require('./utils')
{BlockRange} = require('./range')
extend = require('extend')

class Worksheet

	@create:(workbook, id, rid, name, xml)->
		parseStringAsync(xml, {tagNameProcessors: [processors.stripPrefix]})
		.then (result)->
			new Worksheet(workbook, id, rid, name, result)

	constructor:(workbook, id, rid, name, xmlobj)->
		@_ = {}
		@_.workbook = workbook
		@_.xmlobj = xmlobj
		ws = xmlobj.worksheet
		@_.id = parseInt id
		@_.rid = parseInt rid
		@_.name = name
		@_.dimension = new BlockRange(ws.dimension[0].$.ref)
		@defaultColWidth = (parseFloat ws.sheetFormatPr[0].$.defaultColWidth) || 12
		@defaultRowHeight = (parseFloat ws.sheetFormatPr[0].$.defaultRowHeight) || 20

		extLst = ws.extLst?[0]
		if extLst
			for ext in extLst.ext
				delete ext.$
				for k,v of ext
					ws[k] = [] unless ws[k]
					ws[k] = ws[k].concat(v)
		delete ws.extLst

		@validations = []
		if ws.dataValidations
			for dataValidations in ws.dataValidations
				for v in dataValidations.dataValidation
					@validations.push new Validation(v)


		@_.mergedCells = []
		mergedCells = ws.mergeCells?[0].mergeCell
		if mergedCells
			for cell in mergedCells
				@_.mergedCells.push new BlockRange(cell.$.ref)

		@_.rows = []
		if ws.sheetData[0]
			for row_obj in ws.sheetData[0].row
				row = row_obj.$.r
				@_.rows[row] = new Row(this, row, row_obj)


	Object.defineProperties @prototype,
		"workbook":
			get: -> @_.workbook
		"id":
			get: -> @_.id
		"rid":
			get: -> @_.rid
		"name":
			get: -> @_.name
		"top":
			get: -> @_.dimension.top
		"left":
			get: -> @_.dimension.left
		"bottom":
			get: -> @_.dimension.bottom
		"right":
			get: -> @_.dimension.right


	getRow: (r)->
		throw new Error "Index is out of range" unless r > 0
		@_.rows[r] = @_.rows[r] || new Row(@workbook, this, r)

	getCell:(r, c)->
		@getRow(r).getCell(c)

	merge:(top, left, bottom, right)-><commit_msg>Fix constructor arguments<commit_after>Promise = require('bluebird')
{parseStringAsync} = Promise.promisifyAll(require('xml2js'))
processors = require('xml2js/lib/processors')
Row = require('./row')
Validation = require('./validation')
Utils = require('./utils')
{BlockRange} = require('./range')
extend = require('extend')

class Worksheet

	@create:(workbook, id, rid, name, xml)->
		parseStringAsync(xml, {tagNameProcessors: [processors.stripPrefix]})
		.then (result)->
			new Worksheet(workbook, id, rid, name, result)

	constructor:(workbook, id, rid, name, xmlobj)->
		@_ = {}
		@_.workbook = workbook
		@_.xmlobj = xmlobj
		ws = xmlobj.worksheet
		@_.id = parseInt id
		@_.rid = parseInt rid
		@_.name = name
		@_.dimension = new BlockRange(ws.dimension[0].$.ref)
		@defaultColWidth = (parseFloat ws.sheetFormatPr[0].$.defaultColWidth) || 12
		@defaultRowHeight = (parseFloat ws.sheetFormatPr[0].$.defaultRowHeight) || 20

		extLst = ws.extLst?[0]
		if extLst
			for ext in extLst.ext
				delete ext.$
				for k,v of ext
					ws[k] = [] unless ws[k]
					ws[k] = ws[k].concat(v)
		delete ws.extLst

		@validations = []
		if ws.dataValidations
			for dataValidations in ws.dataValidations
				for v in dataValidations.dataValidation
					@validations.push new Validation(v)


		@_.mergedCells = []
		mergedCells = ws.mergeCells?[0].mergeCell
		if mergedCells
			for cell in mergedCells
				@_.mergedCells.push new BlockRange(cell.$.ref)

		@_.rows = []
		if ws.sheetData[0]
			for row_obj in ws.sheetData[0].row
				row = row_obj.$.r
				@_.rows[row] = new Row(this, row, row_obj)


	Object.defineProperties @prototype,
		"workbook":
			get: -> @_.workbook
		"id":
			get: -> @_.id
		"rid":
			get: -> @_.rid
		"name":
			get: -> @_.name
		"top":
			get: -> @_.dimension.top
		"left":
			get: -> @_.dimension.left
		"bottom":
			get: -> @_.dimension.bottom
		"right":
			get: -> @_.dimension.right


	getRow: (r)->
		throw new Error "Index is out of range" unless r > 0
		@_.rows[r] = @_.rows[r] || new Row(this, r)

	getCell:(r, c)->
		@getRow(r).getCell(c)

	merge:(top, left, bottom, right)->
{'fraction_non_alphanumeric': 0.16607185343802047, 'fraction_numerical': 0.006661908160837497, 'mean_word_length': 3.481876332622601, 'pattern_counts': {'":': 16, '<': 3, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 16}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1030814', 'n_tokens_mistral': 1770, 'n_tokens_neox': 1499, 'n_words': 354}