Пример #1
0
        public override ReferenceReader Initialize()
        {
            LoadCache();

            if (!IsAllDataCached() || CardMakerInstance.ForceDataCacheRefresh)
            {
                var zSpreadsheet =
                    new GoogleSpreadsheet(CardMakerInstance.GoogleInitializerFactory);
                try
                {
                    zSpreadsheet.MakeSimpleSpreadsheetRequest();
                }
                catch (GoogleApiException e)
                {
                    if (GoogleApi.IsAuthorizationError(e))
                    {
                        CardMakerInstance.GoogleCredentialsInvalid = true;
                    }
                }
                catch (Exception e)
                {
                    Logger.AddLogLine("Google Access Error: {0}".FormatString(e.Message));
                }
            }

            return(this);
        }
Пример #2
0
        static void Main(string[] args)
        {
            UserCredential credential;

            using (var stream = new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
            {
                string credPath = System.Environment.GetFolderPath(
                    System.Environment.SpecialFolder.Personal);
                credPath = Path.Combine(credPath, ".credentials/sheets.googleapis.com-dotnet-quickstart.json");

                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    Scopes,
                    "user",
                    CancellationToken.None,
                    new FileDataStore(credPath, true)).Result;
                Console.WriteLine("Credential file saved to: " + credPath);
            }

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

            var googleSpreadSheet = new GoogleSpreadsheet("1eKY_4hPVlsBRcHkYfTHJxMOXT_nb4NlAy7kOaBOaXuk", service);
            var orders            = googleSpreadSheet.GetCurrentOrders();

            IMailService mailService = new GoogleEmailService();

            // Foodie spreadsheet:
        }
Пример #3
0
 static void Main(string[] args)
 {
     var google = new GoogleSpreadsheet("", "",0);
     var list = new List<int> { 9,8,7,6,5,4,3,2,1};
     var ints = google.GetCellListInt();
     foreach (int i in ints)
     {
         Console.WriteLine(i);
     }
     //google.ClearSheet();
     google.setCells(google.ToStringList(list), new SheetDimensions(list.Count(),1));
     Console.Read();
 }
Пример #4
0
        public GoogleReferenceReader()
        {
            m_zSpreadsheetsService = GoogleSpreadsheet.GetSpreadsheetsService(APP_NAME, CLIENT_ID,
                                                                              CardMakerInstance.GoogleAccessToken);

            LoadCache();

            if (!IsAllDataCached() || CardMakerInstance.ForceDataCacheRefresh)
            {
                // local cache is not enough to load this reference, check token access
                if (!GoogleApi.VerifyAccessToken(CardMakerInstance.GoogleAccessToken))
                {
                    CardMakerInstance.GoogleCredentialsInvalid = true;
                }
            }
        }
Пример #5
0
        public GoogleReferenceReader()
        {
            LoadCache();

            if (!IsAllDataCached() || CardMakerInstance.ForceDataCacheRefresh)
            {
                var zSpreadsheet =
                    new GoogleSpreadsheet(CardMakerInstance.GoogleInitializerFactory);
                try
                {
                    zSpreadsheet.MakeSimpleSpreadsheetRequest();
                }
                catch (GoogleApiException e)
                {
                    if (GoogleApi.IsAuthorizationError(e))
                    {
                        CardMakerInstance.GoogleCredentialsInvalid = true;
                    }
                }
            }
        }
        public void GetData(GoogleSpreadsheetReference zReference, List <List <string> > listData, bool bRemoveFirstRow, string sNameAppend = "")
        {
            var sCacheKey = GetCacheKey(zReference.generateFullReference(), sNameAppend);
            List <List <string> > listCacheData;

            if (!CardMakerInstance.ForceDataCacheRefresh && m_dictionaryDataCache.TryGetValue(sCacheKey, out listCacheData))
            {
                ProgressReporter.AddIssue("Loading {0} from local cache".FormatString(sCacheKey));
                listData.AddRange(listCacheData);
                return;
            }

            var sSpreadsheetName = zReference.SpreadsheetName;
            var sSheetName       = zReference.SheetName + sNameAppend;

            var bAuthorizationError = false;
            var bError = false;

            List <List <string> > listGoogleData = null;

            try
            {
                var zGoogleSpreadsheet = new GoogleSpreadsheet(CardMakerInstance.GoogleInitializerFactory);
                if (string.IsNullOrWhiteSpace(zReference.SpreadsheetId))
                {
                    ProgressReporter.AddIssue("WARNING: The reference {0}.{1} is missing the Spreadsheet ID. Please reconfigure this reference."
                                              .FormatString(zReference.SpreadsheetName, zReference.SheetName));
                    listGoogleData = zGoogleSpreadsheet.GetSheetContentsBySpreadsheetName(sSpreadsheetName, sSheetName);
                }
                else
                {
                    listGoogleData = zGoogleSpreadsheet.GetSheetContentsBySpreadsheetId(zReference.SpreadsheetId, sSheetName);
                }

                // blank data just means an empty or non-existent sheet (generally okay)
                if (listGoogleData == null)
                {
                    listGoogleData = new List <List <string> >();
                }
            }
            catch (GoogleApiException e)
            {
                ProgressReporter.AddIssue("Google Spreadsheet access exception: " + e.Message);
                bAuthorizationError = GoogleApi.IsAuthorizationError(e);
            }
            catch (Exception e)
            {
                ProgressReporter.AddIssue("General exception: " + e.Message);
                listGoogleData = null;
                bError         = true;
            }
            if (bAuthorizationError || bError || listGoogleData == null)
            {
                ProgressReporter.AddIssue("Failed to load any data from Google Spreadsheet." + "[" + sSpreadsheetName + "," + sSheetName + "]" + (bAuthorizationError ? " Google reported a problem with your credentials." : String.Empty));
            }
            else
            {
                if (bRemoveFirstRow && listGoogleData.Count > 0)
                {
                    listGoogleData.RemoveAt(0);
                }

                listData.AddRange(listGoogleData);
                if (m_dictionaryDataCache.ContainsKey(sCacheKey))
                {
                    m_dictionaryDataCache.Remove(sCacheKey);
                }
                m_dictionaryDataCache.Add(sCacheKey, listGoogleData);
                m_bCacheUpdated = true;
            }
        }
Пример #7
0
        public void GetData(string sGoogleReference, List <List <string> > listData, bool bRemoveFirstRow, string sNameAppend = "")
        {
            var arraySettings = sGoogleReference.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

            if (arraySettings.Length < 3)
            {
                return;
            }

            string sCacheKey = GetCacheKey(sGoogleReference, sNameAppend);
            List <List <string> > listCacheData;

            if (!CardMakerInstance.ForceDataCacheRefresh && m_dictionaryDataCache.TryGetValue(sCacheKey, out listCacheData))
            {
                Logger.AddLogLine("Loading {0} from local cache".FormatString(sCacheKey));
                listData.AddRange(listCacheData);
                return;
            }

            var sSpreadsheetName = arraySettings[1];
            var sSheetName       = arraySettings[2] + sNameAppend;

            var bCredentialsError = false;

            List <List <String> > listGoogleData;

            try
            {
                listGoogleData = GoogleSpreadsheet.GetSpreadsheet(m_zSpreadsheetsService, sSpreadsheetName, sSheetName);
            }
            catch (InvalidCredentialsException e)
            {
                Logger.AddLogLine("Credentials exception: " + e.Message);
                bCredentialsError = true;
                listGoogleData    = null;
            }
            catch (Exception e)
            {
                Logger.AddLogLine("General exception: " + e.Message);
                listGoogleData = null;
            }

            if (null == listGoogleData)
            {
                Logger.AddLogLine("Failed to load data from Google Spreadsheet." + "[" + sSpreadsheetName + "," + sSheetName + "]" + (bCredentialsError ? " Google reported a problem with your credentials." : string.Empty));
            }
            else
            {
                if (bRemoveFirstRow && listGoogleData.Count > 0)
                {
                    listGoogleData.RemoveAt(0);
                }

                listData.AddRange(listGoogleData);
                if (m_dictionaryDataCache.ContainsKey(sCacheKey))
                {
                    m_dictionaryDataCache.Remove(sCacheKey);
                }
                m_dictionaryDataCache.Add(sCacheKey, listGoogleData);
                m_bCacheUpdated = true;
            }
        }