• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Change cross-reference format programmatically

Community Expert ,
May 23, 2022 May 23, 2022

Copy link to clipboard

Copied

I want to do this:

  • Find any cross-reference (or x-ref of particular format)
  • Change format to another one defined: e.g. from "heading & chapter-page" to "heading & page".

After finding a cross-reference I have a Text Range of that x-ref. But how to get to the x-ref object?

IMHO there should be an object with propery  XRefFmt

 

aTextItems = oDoc.GetTextForRange(oTR, Constants.FTI_XRefBegin);

 

is of no help here, as it just provides the text of the x-ref.

TOPICS
Scripting

Views

160

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , May 23, 2022 May 23, 2022

Hi Klaus, see if this helps you get started. -Rick

var doc, textRange, textList, xref;

// Set a variable for the active document.
doc = app.ActiveDoc;

// Set a variable for the selected text range in the document.
textRange = doc.TextSelection;

// Get a list of cross-references in the selected text.
textList = doc.GetTextForRange (textRange, Constants.FTI_XRefBegin);
// Make sure there is a cross-reference in the selection.
if (textList.length > 0) {
    // Get the first cross-reference and d
...

Votes

Translate

Translate
Community Expert ,
May 23, 2022 May 23, 2022

Copy link to clipboard

Copied

Hi Klaus, see if this helps you get started. -Rick

var doc, textRange, textList, xref;

// Set a variable for the active document.
doc = app.ActiveDoc;

// Set a variable for the selected text range in the document.
textRange = doc.TextSelection;

// Get a list of cross-references in the selected text.
textList = doc.GetTextForRange (textRange, Constants.FTI_XRefBegin);
// Make sure there is a cross-reference in the selection.
if (textList.length > 0) {
    // Get the first cross-reference and display its source text and format.
    xref = textList[0].obj;
    alert (xref.XRefSrcText);
    alert (xref.XRefFmt.Name);
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 24, 2022 May 24, 2022

Copy link to clipboard

Copied

LATEST

Thank You Rick for pointing me to the right direction!

First I tried this:

aTextItems = oDoc.GetTextForRange(oTR, Constants.FTI_XRefBegin);
if (aTextItems.len > 0) {
  oXref = aTextItems[0].obj;
}
oXref.XRefFmt.Name = sName;   // the new format name
oXref.XRefFmt.Fmt = sDef;     // the new format definition
oDoc.UpdateXRef(oDoc, oXref); // Establish the new xref

But this works only partially: The x-ref in the doc is changed to the new format. but the x-ref format defintion becomes wrong: name: is the old one, definintion. is the new one. After all the old format is renamed to the new format in the catalogue!

This works as intended

aTextItems = oDoc.GetTextForRange(oTR, Constants.FTI_XRefBegin);
if (aTextItems.len > 0) {
 oXref = aTextItems[0].obj;
}
sXRefID = oXref.XRefSrcText;
oDoc.Clear(0); // clear current selection
oTR = oDoc.TextSelection;
oXRef = oDoc.NewAnchoredFormattedXRef(sName, oTR.beg); // Insert a new xref object
oXRef.XRefSrcIsElem = false; // make it an unstructured xref (is default, leave out)
oXRef.XRefFile = oDoc.Name;  // required also for 'in-document' xref?
oXRef.XRefSrcText = sXRefID;
oDoc.UpdateXRef(oDoc, oXref); // Establish the new xref.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines