Exemple #1
0
        /// <summary>
        /// Gets all the charts in the specified Excel workbook and presents them in a view.
        /// </summary>
        /// <param name="id">The internal ID of the workbook.</param>
        /// <returns>The view with the list of charts.</returns>
        public async Task <ActionResult> Index(string id)
        {
            // Get access token from the local database
            var token = Data.GetUserSessionToken(Settings.GetUserAuthStateId(ControllerContext.HttpContext), Settings.AzureADAuthority);

            var sheetsUrl = GraphApiHelper.GetSheetsWithChartsUrl(id, "&$select=name,id");
            var sheets    = await ODataHelper.GetItems <ExcelSheet>(sheetsUrl, token.AccessToken);

            // Merge the charts from each worksheet into a single list
            List <Chart> allChartsInWorkbook = new List <Chart>();

            foreach (var sheet in sheets)
            {
                var chartsFromSheet = sheet.Charts;

                // The GetChartImage method requires a clean charts URL, that is, no $select option.
                string cleanFullChartsUrl = GraphApiHelper.GetChartsUrl(id, sheet.Id, null);

                foreach (var chart in chartsFromSheet)
                {
                    // string singleChartImageUrl = GraphApiHelper.GetSingleChartImageUrl(cleanFullChartsUrl, chart.Id);
                    chart.ImageAsBase64String = await GraphApiHelper.GetChartImage(cleanFullChartsUrl, chart.Id, token.AccessToken);
                }

                allChartsInWorkbook = allChartsInWorkbook.Concat(chartsFromSheet).ToList();
            }
            return(View(allChartsInWorkbook));
        }