How to generate analysis report by opening an existing project programmatically via API

Hi,

I want to use Trados API to do project automation. Currently I am familiar with many tasks.

But for analysis report, I have a question. 

Currently, If I run a new project, I know using the following method to generate a report out.

AutomaticTask analyzeTask = project.RunAutomaticTask(targetFiles.GetIds(), AutomaticTaskTemplateIds.AnalyzeFiles);

Guid reportId = analyzeTask.Id;
project.SaveTaskReportAs(reportId, filePath, Sdl.ProjectAutomation.Core.ReportFormat.Excel);

But I want just to open an existing project and only want to generate the analysis report, how to to do that, I don't know how to get the report ID without running the analysis (the exisitng project has run the analysis task).

I am using two versions, Trados 2019 and Trados 2024, both version do not have such API doc about this part.

Thanks,

  • Hi Flavio,

    you can access an existing project via the ProjectController (the project list in Studio), or via opening a stored file based project:


    var projController = SdlTradosStudio.Application.GetController<ProjectsController>();
    var proj = projController.CurrentProject;

    or

    proj = new FileBasedProject(projectFilePath);

    After that, you can access various project information, including the already generated statistics:

    var pi = proj.GetProjectStatistics();
    var tc = pi.TargetLanguageStatistics[0];
    var ana = tc.AnalysisStatistics;
    var pinfo = proj.GetProjectInfo();
    pName = pinfo.Name;

    Unfortunately, as far as I know you cannot directly save this statistics as an Excel report. You can generate your own though, maybe populate an Excel template via OpenXML or Excel Interop.
    The following code simply concats all info for a MessageBox but it should not be a big problem using the same info to populate any other grid or template:

    txt =
    $"Project: {pinfo.Name}\r\nTotal words: {tc.AnalysisStatistics.Total.Words}\r\Comprising Rep: {tc.AnalysisStatistics.Repetitions.Words}\r\n100+CM: {tc.AnalysisStatistics.Exact.Words + tc.AnalysisStatistics.Perfect.Words + tc.AnalysisStatistics.InContextExact.Words}\r\nFuzzy:";
    foreach (var fcd in tc.AnalysisStatistics.Fuzzy)
    {
    txt += $"{fcd.Band.MinimumMatchValue}-{fcd.Band.MaximumMatchValue}: {fcd.Words}\r\n";
    }

    Would this work for you?

    Best,
    Andreas

  • Hi  , the API doesn't expose a direct way to programmatically extract the existing analysis task and its report without re-running the analysis.  You can however recover the taskId from the generated analysis.xml reports that are in the project folder `Reports\`.  

    Example:

    using System;
    using System.Xml;
    
    public static class ReportXmlHandler
    {
        public static string GetTaskIdFromReport(string filePath)
        {
            if (string.IsNullOrEmpty(filePath))
                throw new ArgumentException("filePath cannot be null or empty.", nameof(filePath));
    
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(filePath);
    
                XmlNode taskInfoNode = xmlDoc.SelectSingleNode("//task/taskInfo");
    
                return taskInfoNode?.Attributes?["taskId"]?.Value;
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error reading XML: {ex.Message}");
                return null;
            }
        }
    }

    Patrick Andrew Hartnett | Developer Experience | Team Lead | RWS Group

  • Hi Andreas, 

    Thank you for the solution. I will try this script to see if I can get what we expect.

  • Hi  ,

    Thank you for reply. I am wondering after getting the taskId, what I should do to get the analysis task or report, do you have more suggestions?

  • Hi  , I miss-read yr initial question:-(  you can access the reports from an API that was introduced recently: Working with Project Reports

    Example:

    var reports = new ProjectReportsOperations(fileBasedProject).GetProjectReports();


    Note: You should also make reference to the Reports View Plus project from our public github repository. It'll provide you with a good example of how to work with this API

    Patrick Andrew Hartnett | Developer Experience | Team Lead | RWS Group