Skip to main content
Known Participant
April 5, 2023
Question

Get Max. Panel based on Table Height

  • April 5, 2023
  • 1 reply
  • 214 views

Hi,

I wanted to create a panel with 'n' number of columns based on the table columns. But as of now I have get only 13 columns. Please guide me to add columns as many as based on the table column length.

		var sCancel = { en: "Cancel"};
    if ( app.activeDocument ) {
	//app.doScript( autoSpalte, undefined, undefined, UndoModes.ENTIRE_SCRIPT, localize(sAutoColumn) );
var tables = app.activeDocument.stories.everyItem().tables.everyItem();
	for(myCounter = 0; myCounter < app.activeDocument.activeLayer.textFrames.length; myCounter++)
	{
        // alert("TextFrame : " + app.activeDocument.activeLayer.textFrames.item(myCounter).contents);
         myStory = app.activeDocument.activeLayer.textFrames.item(myCounter);
      myStory.fit(FitOptions.frameToContent); 
      //  alert(myStory.fit(FitOptions.frameToContent))
		if (myStory.constructor.name == "TextFrame")
		{  
		for( var j = 0; j< myStory.tables.length ; j++ ) 
		{
      myStory.fit(FitOptions.frameToContent); 
      
            var myColumnCount = myStory.tables[j].columns.length
					for( var i = 0; i< myStory.tables[j].columns.length ;i++ ) 
			{
           //     alert(myStory.tables[j].columns.length)
          // alert("Width" + myStory.textFramePreferences.textColumnFixedWidth)
          // alert("Left"+ myStory.tables[j].cells[i].leftInset)
           //alert("Right" + myStory.tables[j].cells[i].rightInset)
           if (myStory.constructor.name == "TextFrame")
		{  
		myFrameWidth = myStory.textFramePreferences.textColumnFixedWidth-myStory.tables[j].cells[i].leftInset-myStory.tables[j].cells[i].rightInset;
       }
	 else {
		// 210208 Edit: try to get column width instead of simply textframe width
		try {
         //   alert(myStory)
			myFrameWidth = myFrame.textFramePreferences.textColumnFixedWidth;
            
		} catch(e) {
			myFrameWidth = myStory.geometricBounds[3] - myStory.geometricBounds[1];
		}
     
	}

                  }
    
	}
}
}
    }
    var nuColumnWidths = new Array();
	var nuColumnWidthFields = new Array();
	var nuColumnWidthFieldLabels = new Array();
	var nuColumnWidthCalc = new Array();
	
    var w = new Window("dialog", "Column Fitting");
	w.orientation = "row";
	w.alignChildren = "fill";
	w.alignment = "fill";
	var fields = w.add("group");
	fields.orientation = "row";
	fields.alignChildren = "fill";
	fields.alignment = "fill";
    for (var n = 0; n < myColumnCount; n++) {
		nuColumnWidths[n]= tables.columns[n].width;
		var aPanel = fields.add("panel");
		aPanel.orientation = "column";
		aPanel.alignChildren="top";
		aPanel.ix = n;
		aPanel.text =  n.toString(); 

        nuColumnWidths[n] = '';
         	nuColumnWidthFields[n] = aPanel.add("edittext", undefined, nuColumnWidths[n]);
            nuColumnWidthFields[n].characters = 2;
		nuColumnWidthFields[n].onChange = function() {
			var ix = this.parent.ix;
 			nuColumnWidths[ix] = Number(nuColumnWidthFields[ix].text.replace(/,/g, "."));
         // alert(nuColumnWidths[ix] )
			nuColumnWidthCalc[ix].value = false;
			calcAuto();
		}
	
		nuColumnWidthCalc[n] = aPanel.add("statictext", undefined, "");
		nuColumnWidthCalc[n].value = false;
		nuColumnWidthCalc[n].onClick = function() {
			calcAuto();
           
		}
	}
	//calcAuto();
		w.buttons = w.add("group");
	w.buttons.orientation = "column";
	w.buttons.alignChildren = "left";
	w.defaultElement = w.buttons.add("button", undefined, "OK");
//~ 		w.defaultElement.onClick = okClick;
	w.cancelElement = w.buttons.add( "button", undefined, localize(sCancel) );
	w.cancelElement.onClick = function() {
		this.window.close();
		exit();
	}
	
	w.center();
	var bClickedOK = w.show();

 

This topic has been closed for replies.

1 reply

Community Expert
April 12, 2023

Please improve your code and explain clearly what you are trying to do in a better way. Some pointers that are causing confusion reading the code are

  • You save textframe object inside a variable named myStory
  • You have lots of commented code littering around. Share only the portion that is relevant to the issue
  • You have unnecessary code, you check if my Story is a textframe and then within the if body you again check if it's a textframe, is this needed?

For explaining the issue you are facing, describe what your code currently does and what you expect it to do instead. Some screenshots, sample document etc would be of great assistance

-Manan

-Manan