示例#1
0
        public static string InitTreeView(string accessToken, string refreshToken)
        {
            try
            {
                // Make the Auth request to Google
                SpreadsheetsService sheetsService = GoogleOAuth2.GoogleAuthSheets(accessToken, refreshToken);
                if (sheetsService == null)
                {
                    return("");
                }

                // Get list of sheets
                DriveService driveService = GoogleOAuth2.GoogleAuthDrive(accessToken, refreshToken);
                if (driveService == null)
                {
                    return("");
                }
                List <GoogleSheet> sheetList = GoogleDriveHelpers.GoogleRetrieveAllSheets(sheetsService);
                GoogleFolder       root      = GoogleDriveHelpers.GoogleRetrieveSheetTree(driveService, null, ref sheetList);

                System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(root.GetType());
                //using (var stream = new StreamWriter(@"", false))
                //{
                //    x.Serialize(stream, root);
                //}

                string xmlStr = GoogleDriveHelpers.SerializeXml <GoogleFolder>(root);
                xmlStr = xmlStr.Replace("<Children>", "").Replace("<Children />", "").Replace("</Children>", "");
                xmlStr = xmlStr.Replace("<Sheets>", "").Replace("<Sheets />", "").Replace("</Sheets>", "");

                return(xmlStr);
            }
            catch (Exception ex)
            {
                return("");
            }
        }
        /// <summary>
        /// Submit button handler. Creates file contents and adds to Google Drive.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void SubmitGoogle(object sender, EventArgs e)
        {
            try
            {
                string newDocumentName = NewDocument.Text;
                string selectedUri     = "";
                bool   isSheet         = false;

                // check for valid user selection first
                if (SheetTree.SelectedNode == null)
                {
                    selectedUri = "root";
                }
                else
                {
                    selectedUri = SheetTree.SelectedNode.Value;
                    if (String.IsNullOrWhiteSpace(selectedUri))
                    {
                        throw new Exception("Server exception: code 4");
                    }
                    isSheet = (selectedUri.Contains("http://") || selectedUri.Contains("https://"));
                }

                string submitData  = SubmitDataStr;
                string meetingName = MeetingNameDate;

                if (String.IsNullOrWhiteSpace(submitData) ||
                    (String.IsNullOrWhiteSpace(newDocumentName) && String.IsNullOrWhiteSpace(selectedUri)) ||
                    (String.IsNullOrWhiteSpace(newDocumentName) && !isSheet))
                {
                    throw new Exception("Missing data, document name or selected document/folder.");
                }

                // Make the Auth request to Google
                SpreadsheetsService sheetService = GoogleOAuth2.GoogleAuthSheets(Page.Request);
                if (sheetService == null)
                {
                    throw new Exception("Google authentication not acquired. Try logging off and in again.");
                }

                // Make the Auth request to Google
                DriveService driveService = GoogleOAuth2.GoogleAuthDrive(Page.Request);
                if (driveService == null)
                {
                    throw new Exception("Google authentication not acquired. Try logging off and in again.");
                }

                // create file contents
                List <CheckinEntry> entries = GoogleDriveHelpers.CreateFileContent(submitData);
                if (entries == null)
                {
                    throw new Exception("Server exception: code 5");
                }

                // create new spreadsheet if need be
                if (!String.IsNullOrWhiteSpace(newDocumentName) && !isSheet)
                {
                    GDrive.File newFile = GoogleDriveHelpers.CreateFile(driveService, newDocumentName, selectedUri);
                    if (newFile == null)
                    {
                        throw new Exception("Server exception: code 6");
                    }
                    // get spreadsheet feed
                    List <GoogleSheet> sheetList = GoogleDriveHelpers.GoogleRetrieveAllSheets(sheetService);
                    selectedUri = "";
                    if (sheetList != null && sheetList.Count > 0)
                    {
                        selectedUri = (from feed in sheetList where feed.Id.Equals(newFile.Id) select feed.FeedUri).First();
                    }
                }

                // create new worksheet in spreadsheet
                WorksheetEntry worksheet = GoogleDriveHelpers.CreateWorksheet(sheetService, selectedUri, meetingName);
                if (worksheet == null)
                {
                    throw new Exception("Server exception: code 7");
                }

                // Add content to worksheet
                if (!GoogleDriveHelpers.AddContentToWorksheet(sheetService, worksheet, entries))
                {
                    throw new Exception("Server exception: code 8");
                }

                // output page redirect
                string script = string.Format("alert('Data added successfully.');");
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "alert", script, true);
            }
            catch (Exception ex)
            {
                string script = string.Format("alert('Request not logged: {0}');", ex.Message);
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "alert", script, true);
            }
        }