public RunWebCommandResult Execute(ImportFromGoogleSheetParameters importParameters)
        {
            GoogleCredential credential;

            credential = GoogleCredential
                         .FromFile(importParameters.CredentialsFileNamePath)
                         .CreateScoped(Scopes);

            // Create Google Sheets API service.
            var service = new SheetsService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName       = _applicationName,
            });

            // Define request parameters.
            string spreadsheetId = importParameters.SpreadsheetId;
            string range         = importParameters.ImportRange;

            SpreadsheetsResource.ValuesResource.GetRequest request =
                service.Spreadsheets.Values.Get(spreadsheetId, range);

            ValueRange response            = request.Execute();
            IList <IList <object> > values = response.Values;

            if (values != null && values.Count > 0)
            {
                Console.WriteLine("- Reading data from Google sheet: -");
                for (int row = 0; row < values.Count; row++)
                {
                    Console.Write("[row {0}] ", row);
                    for (int col = 0; col < values[row].Count; col++)
                    {
                        Console.Write("|{1,5}", col, values[row][col]);
                    }
                    Console.WriteLine();
                }
            }
            else
            {
                Console.WriteLine("No data found.");
            }
            //Console.Read();

            return(new RunWebCommandResult()
            {
                ResultCode = 0
            });
        }
Esempio n. 2
0
        public void ImportData()
        {
            var import           = new ImportFromGoogleSheetToExcel();
            var importParameters = new ImportFromGoogleSheetParameters()
            {
                SpreadsheetId           = "1DVVmqRWVTn4nQkwVOd5VOucQ_L4fTT4S3EiVR-W13qA",
                ImportRange             = "Sheet1!A1:E",
                CredentialsFileNamePath = @"C:\git\ParserQueueRunner\credentials.json",
                ExcelInsertRange        = ""
            };

            var result = import.Execute(importParameters);

            Assert.IsNotNull(result);
            Assert.AreEqual(0, result.ResultCode);
        }