Пример #1
0
        public Entry GetRandomEntry()
        {
            using var table = new ExcelWorksheetWrapper(Path, Worksheet, TableName);
            var rows           = table.EnumerateRows().ToList();
            var rowCount       = rows.Count;
            var randomRowIndex = new Random().Next(0, rowCount);
            var dateCell       = table.GetCellValue(randomRowIndex, table.DateColIndex);

            return(CreateEntryAtRowIndex(table, (DateTime)dateCell, randomRowIndex));
        }
Пример #2
0
        private Entry CreateEntryAtRowIndex(ExcelWorksheetWrapper table, DateTime entryDate, int row)
        {
            var content = table.GetCellValue(row, table.ContentColIndex)?.ToString();

            // Ignore cells with empty contents
            if (string.IsNullOrWhiteSpace(content))
            {
                return(null);
            }

            return(new Entry
            {
                Date = entryDate,
                Content = content,
                Keyword = table.GetCellValue(row, table.KeywordColIndex)?.ToString(),
                Mood = table.GetCellValue(row, table.MoodColIndex)?.ToString(),
                Remarks = table.GetCellValue(row, table.RemarksColIndex)?.ToString(),
            });
        }
Пример #3
0
        public Entry GetEntry(DateTime date)
        {
            using var table = new ExcelWorksheetWrapper(Path, Worksheet, TableName);

            // Look for entry with specified date
            foreach (var row in table.EnumerateRows())
            {
                var dateCell = table.GetCellValue(row, table.DateColIndex);
                if (dateCell is DateTime entryDate && entryDate.Date == date.Date)
                {
                    return(CreateEntryAtRowIndex(table, entryDate, row));
                }
            }

            // Return null if not found
            return(null);
        }
Пример #4
0
        public IEnumerable <Entry> GetEntries(DateTime startDate, DateTime endDate)
        {
            using var table = new ExcelWorksheetWrapper(Path, Worksheet, TableName);

            // Look for entry with specified date
            foreach (var row in table.EnumerateRows())
            {
                var dateCell = table.GetCellValue(row, table.DateColIndex);
                if (dateCell is DateTime entryDate && entryDate.Date >= startDate.Date && entryDate.Date <= endDate)
                {
                    var entry = CreateEntryAtRowIndex(table, entryDate, row);
                    if (entry != null)
                    {
                        yield return(entry);
                    }
                }
            }
        }