<commit_before>var assign = require('lodash.assign');
var each = require('lodash.foreach');
var mapKeys = require('lodash.mapkeys');
var util = require('util');

var definedNames = require('../definitions/definedNames');
var templates = require('./templates');
var utilities = require('../utilities');
var Part = require('./partWithRels');
var Worksheet = require('./worksheet');
var SharedStrings = require('./sharedStrings');

function Workbook(data) {
    this.initialize(data);
    Part.call(this, {
        spreadsheetml: '__main',
        relationships: 'r'
    }, templates.workbook);
};

util.inherits(Workbook, Part);

module.exports = Workbook;

assign(Workbook.prototype, {
    initialize: function (data) {
        var haveData = utilities.isObject(data);
        if (haveData && data.sheets) {
            this.addSheets(data.sheets);
        }
        if (haveData && data.styles) {
            this.addStyles(data.styles);
        }
    },
    addDefinedName: function (name, value, sheet) {
        this.definedNames[name] = {value: value, id: sheet.id};
    },
    addSharedString: function (string) {
        return this.sharedStrings.add(string);
    },
    addSheets: function (sheets) {
        each(sheets, function (sheet, n) {
            if (utilities.isObject(sheet) && typeof n === 'string') {
                sheet.name = n;
                this.addSheet(sheet);
            } else if (Array.isArray(sheet)) {
                this.addSheet(n, sheet);
            }
        }, this);
    },
    addSheet: function(nameOrData, rows) {
        var payload;
        // if first arg is an object, assume it's a sheet data object
        if (utilities.isObject(nameOrData)) payload = nameOrData;
        // if not, it's likely a name
        if (!payload) payload = {name: nameOrData};
        // if it turns out to be neither, set the name automatically
        if (!payload.name || typeof payload.name !== 'string') {
            payload.name = 'Sheet' + this.sheets.length;
        }
        // If second arg is an array, set it as rows.
        if (utilities.isArray(rows)) payload.rows = rows;
        var sheet = new Worksheet(this, payload);
        this.sheets.push(sheet);
        this.sheetNames.push(sheet.name);
        this.rels.addWorksheet(sheet);
        return sheet;
    },
    getSheet: function(name) {
        return this.sheets[this.sheetNames.indexOf(name)];
    }
});

Object.defineProperties(Workbook.prototype, utilities.defineProps({
    constant: {
        type: 'officeDocument',
        filename: 'workbook.xml'<commit_msg>workbook: implement styles, fix style related bugs<commit_after>var assign = require('lodash.assign');
var each = require('lodash.foreach');
var mapKeys = require('lodash.mapkeys');
var util = require('util');

var definedNames = require('../definitions/definedNames');
var templates = require('./templates');
var utilities = require('../utilities');
var Part = require('./partWithRels');
var Styles = require('./styles');
var Worksheet = require('./worksheet');
var SharedStrings = require('./sharedStrings');

function Workbook(data) {
    this.initialize(data);
    Part.call(this, {
        spreadsheetml: '__main',
        relationships: 'r'
    }, templates.workbook);
};

util.inherits(Workbook, Part);

module.exports = Workbook;

assign(Workbook.prototype, {
    initialize: function (data) {
        var haveData = utilities.isObject(data);
        if (haveData && data.styles) {
            this.addStyles(data.styles);
        }
        if (haveData && data.sheets) {
            this.addSheets(data.sheets);
        }
    },
    addDefinedName: function (name, value, sheet) {
        this.definedNames[name] = {value: value, id: sheet.id};
    },
    addSharedString: function (string) {
        return this.sharedStrings.add(string);
    },
    addSheets: function (sheets) {
        each(sheets, function (sheet, n) {
            if (utilities.isObject(sheet) && typeof n === 'string') {
                sheet.name = n;
                this.addSheet(sheet);
            } else if (Array.isArray(sheet)) {
                this.addSheet(n, sheet);
            }
        }, this);
    },
    addSheet: function(nameOrData, rows) {
        var payload;
        // if first arg is an object, assume it's a sheet data object
        if (utilities.isObject(nameOrData)) payload = nameOrData;
        // if not, it's likely a name
        if (!payload) payload = {name: nameOrData};
        // if it turns out to be neither, set the name automatically
        if (!payload.name || typeof payload.name !== 'string') {
            payload.name = 'Sheet' + this.sheets.length;
        }
        // If second arg is an array, set it as rows.
        if (utilities.isArray(rows)) payload.rows = rows;
        var sheet = new Worksheet(this, payload);
        this.sheets.push(sheet);
        this.sheetNames.push(sheet.name);
        this.rels.addWorksheet(sheet);
        return sheet;
    },
    addStyles: function (styleData) {
        this._styles = new Styles(styleData, this);
        this.rels.addStyles(this._styles);
    },
    getSheet: function(name) {
        return this.sheets[this.sheetNames.indexOf(name)];
    }
});

Object.defineProperties(Workbook.prototype, utilities.defineProps({
    constant: {
        type: 'officeDocument',
        filename: 'workbook.xml'
{'fraction_non_alphanumeric': 0.13766088416340236, 'fraction_numerical': 0.0, 'mean_word_length': 2.5322793148880107, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '4081070', 'n_tokens_mistral': 1566, 'n_tokens_neox': 1544, 'n_words': 376}