Example #1
0
        static async Task <bool> CreateSheetsAsync(this SheetsService service, string spreadsheet, IEnumerable <string> requiredSheets)
        {
            var spreadsheets = await service.GetSpreadsheetAsync(spreadsheet);

            string[] sheetsToCreate = requiredSheets.Except(spreadsheets.GetSheetsList()).ToArray();
            if (sheetsToCreate.Length == 0)
            {
                return(true);
            }

            var result = await service.Spreadsheets.BatchUpdate(AddSheet(sheetsToCreate), spreadsheet).AddBackOffHandler().ExecuteAsync();

            return(result.Replies.All(reply => reply.AddSheet.Properties != null));
        }
Example #2
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);
        }