Ejemplo n.º 1
0
        /// <summary>Read the data from a spreadsheet and deserialize it to object of type T.</summary>
        /// <param name="spreadsheet">Spreadsheet ID.</param>
        /// <param name="sheet">Specify the name of the sheet, if the spreadsheet contains multiple sheets of a type.</param>
        /// <typeparam name="T">Type of object to read from the spreadsheet data.</typeparam>
        /// <returns>Object of type T.</returns>
        public async Task <T> ReadAsync <T>(string spreadsheet, string sheet = "")
        {
            var spreadsheets = await service.GetSpreadsheetAsync(spreadsheet);

            var context = new ReadContext(spreadsheets.GetSheetsList(), serializer);
            var meta    = typeof(T).GetIOAttribute();

            if (!context.TryReadType(meta, meta.AppendNamePart(sheet), out var result))
            {
                throw new Exception("Can't parse the requested object. Some required sheets are missing in the provided spreadsheet");
            }

            var valueRanges = await service.GetValueRanges(spreadsheet, context.Ranges);

            if (!valueRanges.All(context.TryApplyRange))
            {
                throw new Exception("Failed to assemble the object.");
            }
            return((T)result);
        }