Esempio n. 1
0
  // calc the current value of the arithmetic tree
  @Override
  public final void compute() {
    String col =
        GeoElementSpreadsheet.getSpreadsheetColumnName(
            geo.getLabel(StringTemplate.defaultTemplate));

    if (col == null) text.setUndefined();
    else text.setTextString(col);
  }
Esempio n. 2
0
  /**
   * Updates the cell references in text according to a relative copy in the spreadsheet of offset
   * (dx,dy) (changes only dependents of value) eg change A1 < 3 to A2 < 3 for a vertical copy
   */
  public static String updateCellReferences(GeoElement value, String inputText, int dx, int dy) {
    String text = inputText;
    StringBuilder before = new StringBuilder();
    StringBuilder after = new StringBuilder();

    GeoElement[] dependents = getDependentObjects(value);
    GeoElement[] dependents2 = new GeoElement[dependents.length + 1];
    for (int i = 0; i < dependents.length; ++i) {
      dependents2[i] = dependents[i];
    }
    dependents = dependents2;
    dependents[dependents.length - 1] = value;
    for (int i = 0; i < dependents.length; ++i) {
      String name = dependents[i].getLabel(StringTemplate.defaultTemplate);
      MatchResult matcher = GeoElementSpreadsheet.spreadsheetPatternPart.exec(name);
      int column = GeoElementSpreadsheet.getSpreadsheetColumn(matcher);
      int row = GeoElementSpreadsheet.getSpreadsheetRow(matcher);

      if ((column == -1) || (row == -1)) {
        continue;
      }
      String column1 = GeoElementSpreadsheet.getSpreadsheetColumnName(column);
      String row1 = "" + (row + 1);

      before.setLength(0);
      before.append('$');
      before.append(column1);
      before.append(row1);
      after.setLength(0);
      after.append('$');
      after.append(column1);
      after.append("::");
      after.append(row1);
      text =
          replaceAll(
              GeoElementSpreadsheet.spreadsheetPatternPart,
              text,
              before.toString(),
              after.toString());
      // text = replaceAll(AbstractGeoElementSpreadsheet.spreadsheetPatternPart, text,
      // "$" + column1 + row1, "$" + column1 + "::" + row1);

      before.setLength(0);
      before.append(column1);
      before.append('$');
      before.append(row1);
      after.setLength(0);
      after.append("::");
      after.append(column1);
      after.append('$');
      after.append(row1);
      text =
          replaceAll(
              GeoElementSpreadsheet.spreadsheetPatternPart,
              text,
              before.toString(),
              after.toString());
      // text = replaceAll(AbstractGeoElementSpreadsheet.spreadsheetPatternPart, text,
      // column1 + "$" + row1, "::" + column1 + "$" + row1);

      before.setLength(0);
      before.append(column1);
      before.append(row1);
      after.setLength(0);
      after.append("::");
      after.append(column1);
      after.append("::");
      after.append(row1);
      text =
          replaceAll(
              GeoElementSpreadsheet.spreadsheetPatternPart,
              text,
              before.toString(),
              after.toString());
      // text = replaceAll(AbstractGeoElementSpreadsheet.spreadsheetPatternPart, text,
      // column1 + row1, "::" + column1 + "::" + row1);

    }

    // TODO is this a bug in the regex?
    // needed for eg Mod[$A2, B$1] which gives Mod[$A2, ::::B$1]
    // =$B$1 BinomialCoefficient[B$2, $A3] gives BinomialCoefficient[::::::B$2, $A::3]
    text = text.replace("::::::", "::");
    text = text.replace("::::", "::");

    MatchResult matcher =
        GeoElementSpreadsheet.spreadsheetPatternPart.exec(
            value.getLabel(StringTemplate.defaultTemplate));
    for (int i = 0; i < dependents.length; ++i) {
      String name = dependents[i].getLabel(StringTemplate.defaultTemplate);
      matcher = GeoElementSpreadsheet.spreadsheetPatternPart.exec(name);
      int column = GeoElementSpreadsheet.getSpreadsheetColumn(matcher);
      int row = GeoElementSpreadsheet.getSpreadsheetRow(matcher);
      if ((column == -1) || (row == -1)) {
        continue;
      }
      String column1 = GeoElementSpreadsheet.getSpreadsheetColumnName(column);
      String row1 = "" + (row + 1);
      String column2 = GeoElementSpreadsheet.getSpreadsheetColumnName(column + dx);
      String row2 = "" + (row + dy + 1);

      before.setLength(0);
      before.append("::");
      before.append(column1);
      before.append("::");
      before.append(row1);
      after.setLength(0);
      after.append(' '); // space important eg 2 E2 not 2E2
      after.append(column2);
      after.append(row2);
      text = replaceAll(pattern2, text, before.toString(), after.toString());
      // text = replaceAll(pattern2, text, "::" + column1 + "::" + row1,
      // "("+ column2 + row2 + ")");

      before.setLength(0);
      before.append("$");
      before.append(column1);
      before.append("::");
      before.append(row1);
      after.setLength(0);
      after.append('$');
      after.append(column1);
      after.append(row2);
      text = replaceAll(pattern2, text, before.toString(), after.toString());
      // text = replaceAll(pattern2, text, "$" + column1 + "::" + row1,
      // "$" + column1 + row2);

      before.setLength(0);
      before.append("::");
      before.append(column1);
      before.append('$');
      before.append(row1);
      after.setLength(0);
      after.append(column2);
      after.append('$');
      after.append(row1);
      text = replaceAll(pattern2, text, before.toString(), after.toString());
      // text = replaceAll(pattern2, text, "::" + column1 + "$" + row1,
      // column2 + "$" + row1);

    }

    return text;
  }