Esempio n. 1
0
        public GoogleNinjaService(ISpreadsheetConfiguration googleSpreadsheetConfiguration)
        {
            this.googleSpreadsheetConfiguration = googleSpreadsheetConfiguration;

            service = new SpreadsheetsService("tretton37-NinjaBook");
            service.setUserCredentials(googleSpreadsheetConfiguration.Username, googleSpreadsheetConfiguration.Password);
        }
Esempio n. 2
0
        private SheetData CreateExportMultiSheet(ISpreadsheetConfiguration <object> exportConfiguration, out Columns columns)
        {
            //Build out our sheet information
            var    data       = new SheetData();
            UInt32 currentRow = 1;

            if (exportConfiguration.RenderTitle)
            {
                var row = new Row {
                    RowIndex = 1
                };
                var headerCell = new Cell
                {
                    CellReference = $"A{currentRow}",
                    CellValue     = new CellValue(exportConfiguration.DocumentTitle),
                    DataType      = CellValues.String,
                    StyleIndex    = (int)FontStyleIndex.Header
                };
                row.Append(headerCell);
                data.Append(row);
                //Increment row
                currentRow++;
            }

            if (exportConfiguration.RenderSubTitle)
            {
                var row = new Row {
                    RowIndex = currentRow
                };
                var headerCell = new Cell
                {
                    CellReference = $"A{currentRow}",
                    CellValue     = new CellValue(exportConfiguration.DocumentSubTitle),
                    DataType      = CellValues.String,
                    StyleIndex    = (int)FontStyleIndex.SubHeader
                };
                row.Append(headerCell);
                data.Append(row);
                //Increment row
                currentRow++;
            }

            //Run data headers
            var headerProperties = TypeDescriptor.GetProperties(exportConfiguration.DataType);
            var headerRow        = new Row {
                RowIndex = currentRow
            };
            var customFormats = new Dictionary <string, UInt32Value>();

            foreach (PropertyDescriptor prop in headerProperties)
            {
                var headerCell = new Cell
                {
                    CellValue  = new CellValue(prop.DisplayName),
                    DataType   = CellValues.String,
                    StyleIndex = (int)FontStyleIndex.DataHeader
                };
                headerRow.Append(headerCell);

                //Handle formats
                if (prop.Attributes.Count <= 0)
                {
                    continue;
                }
                foreach (var attribute in prop.Attributes)
                {
                    if (!(attribute is SpreadsheetColumnFormatAttribute detail))
                    {
                        continue;
                    }

                    switch (detail.Format.ToLowerInvariant())
                    {
                    case "d":
                        customFormats.Add(prop.DisplayName, (int)FontStyleIndex.NormalDate);
                        break;

                    case "c":
                        customFormats.Add(prop.DisplayName, (int)FontStyleIndex.NormalCurrency);
                        break;
                    }
                    break;
                }
            }

            data.Append(headerRow);
            currentRow++;

            //Run the data
            foreach (var item in exportConfiguration.ExportData)
            {
                var dataRow = new Row {
                    RowIndex = currentRow
                };
                foreach (PropertyDescriptor prop in headerProperties)
                {
                    var itemValue = prop.GetValue(item);
                    var dataCell  = new Cell
                    {
                        CellValue = new CellValue(itemValue?.ToString()),
                        DataType  = CellValues.String
                    };

                    if (customFormats.ContainsKey(prop.DisplayName))
                    {
                        dataCell.StyleIndex = customFormats[prop.DisplayName];
                        if (dataCell.StyleIndex == 4)
                        {
                            dataCell.DataType  = CellValues.Number;
                            dataCell.CellValue = new CellValue(decimal.Parse(itemValue?.ToString()));
                        }
                        else if (dataCell.StyleIndex == 5) //Date
                        {
                            dataCell.CellValue = new CellValue(DateTime.Parse(itemValue.ToString()).ToShortDateString());
                        }
                    }

                    dataRow.Append(dataCell);
                }

                data.Append(dataRow);
                currentRow++;
            }

            //Auto-size
            columns = AutoSize(data);
            return(data);
        }