Scroll

Custom Function: Insert dynamic image

This function allows you to display a different image in a report depending on the value of one or more parameters. For example, a different logo could be shown depending on which user is running the report. This can be useful for canned reports in a multi-tenant environment.

Note: This function requires that the switched images have a similar naming scheme, differing by the value of one or more parameters. See the example below.

Arguments (Min - Max): 1 - 1

References:

  • WebReports.Api.Reports

Usage

LoadDynamicImage(x)

  • x is a string, the path to the images in the following format:

    {path}\someText[param1]moreText[param2].extension

    Note: {path} is relative to the Load Image Prefix setting.

Code

string input = args[0].ToString();
int numParams = 0;

while (true)
{
  // find the location of the parameter
  int start = input.IndexOf('[');
  int end = input.IndexOf(']');

  // if char is not found, IndexOf returns -1
  if (start == -1 || end == -1)
    break;

  numParams++;

  // get the input parameter text
  string inputParam = input.Substring(start + 1, end - (start + 1));
  string paramValue = null;

  // if the parameter exists, get the value
  try { paramValue = sessionInfo.GetParameter(inputParam).Value; }
  catch (NullReferenceException)
  { return "Error: Parameter '" + inputParam + "' not found"; }

  // replace the parameter text with the value
  string left = input.Substring(0, start);
  string right = input.Substring(end + 1);
  input = left + paramValue + right;
}

if (numParams == 0)
  return "Error: No parameters found";

// pass the result to LoadImage
string formulaText = String.Format("=LoadImage(\"{0}\")", input);
CellFormula formula = CellFormula.CreateFormula(sessionInfo.PageInfo, formulaText, CellVariableCollectionFilter.DataField);
return formula.Evaluate(null);

Example

Image files to switch between:

  • logo_ExagoInc.png
  • logo_Google.png
  • logo_Microsoft.png

Function:

  • LoadDynamicImage("logo_[companyId].png")

Hidden Article Information

Article Author
Exago Development
created 2017-06-21 17:24:53 UTC
updated 2017-06-21 21:24:51 UTC

Labels
functions, change, example, custom function, user, switch, parameter, companyId,
Have more questions? Submit a request