使用javascript在InDesign表格单元格中使用条形图

使用javascript在InDesign表格单元格中使用条形图,javascript,progress-bar,adobe-indesign,Javascript,Progress Bar,Adobe Indesign,我在InDesign文档中添加了表,并使用javascript填充了表中的数据。 但在一个地方,我需要根据表格单元格中的百分比显示水平条形图。 下图是所需的输出应如何? 下图是我填充的内容 谁能告诉我怎么做? 下面是我这样做的代码 main(); function main(){ var inputBuild = "/C/Test/xml/A"; var inputIndt = "/C/Test/indt/B"; // R

我在InDesign文档中添加了表,并使用javascript填充了表中的数据。 但在一个地方,我需要根据表格单元格中的百分比显示水平条形图。 下图是所需的输出应如何?

下图是我填充的内容

谁能告诉我怎么做? 下面是我这样做的代码

    main();

    function main(){

        var inputBuild = "/C/Test/xml/A";
        var inputIndt = "/C/Test/indt/B"; 
        // Read indt file object.
        var curFile = File (inputIndt+"/Draft.indt");  
        // check if the file exists  
        if (!curFile.exists) {  
            alert ( "no template!" );  
            exit();  
        }  
        // open the file  
        var curDoc = app.open( curFile );  

        // Reading xml file file 
        xmlFile = new File(inputBuild+"/Build.xml"); 
        if (!xmlFile.exists) exit();      

        xmlFile.open("r");   
        xmlStr = xmlFile.read(); 
        xmlFile.close();  
        root = new XML(xmlStr);

        // Get text frames start
        var curLayer = curDoc.layers[0];

        tfTableFP = curLayer.textFrames.item("Tab_Fund_Position");

        loadDataToTables(root, curLayer, '');

    }

    function loadDataToTables(root, curLayer, reportId){

        var fundPosTable = tfTableFP.tables[0];

        loadDataToFundPosition(root,fundPosTable);
    }

    function loadDataToFundPosition(root,tblName){
        var fundPositionList = root.xpath("/Component/FundPositions/TableData/Item");
        var rowCount = fundPositionList.length();

        if(rowCount != 0){  

            tblName.bodyRowCount = rowCount;

            for(counter = 0; counter < tblName.rows.length; counter++){

                if (counter == 0 || counter == 1){

                }else{
                tblRow = tblName.rows.item(counter);

                component = fundPositionList[counter-2];

                for(cellCounter = 0; cellCounter < tblRow.cells.length; cellCounter++){
                    if(cellCounter == 0){
                        tblString =  component.AssetGroup;
                        tblRow.cells.item(cellCounter).contents =  tblString.toString();
                    }else if(cellCounter == 1){
/*Progress bar/bar graph for negative data to be populated in this cell*/
                        tblString =  component.NetMarketExposure;
                        tblRow.cells.item(cellCounter).contents = tblString.toString();
                    }else if(cellCounter == 2){
/*Progress bar/bar graph for positive data to be populated in this cell*/
                        tblString =  component.StrategicAllocation;
                        tblRow.cells.item(cellCounter).contents = tblString.toString();
                    }else if(cellCounter == 3){
                        tblString =  component.FlexibleOverlay;
                        tblRow.cells.item(cellCounter).contents = tblString.toString();
                    }else if(cellCounter == 4){
                        tblString =  component.NetRange;
                        tblRow.cells.item(cellCounter).contents = tblString.toString();
                    }

                }
                }
            }
        }
    }
main();
函数main(){
var inputBuild=“/C/Test/xml/A”;
var inputIndt=“/C/Test/indt/B”;
//读取indt文件对象。
var curFile=File(inputIndt+“/Draft.indt”);
//检查文件是否存在
如果(!curFile.exists){
警报(“无模板!”);
退出();
}  
//打开文件
var curDoc=app.open(curFile);
//读取xml文件
xmlFile=新文件(inputBuild+“/Build.xml”);
如果(!xmlFile.exists)退出();
xmlFile.open(“r”);
xmlStr=xmlFile.read();
xmlFile.close();
root=新的XML(xmlStr);
//获取文本帧开始
var curLayer=curDoc.layers[0];
tfTableFP=curLayer.textFrames.item(“选项卡资金头寸”);
loadDataToTables(根目录、卷层目录、”);
}
函数loadDataToTables(root、curLayer、reportId){
var Fundpastable=tfTableFP.tables[0];
loadDataToFundPosition(根目录,fundPosTable);
}
函数loadDataToFundPosition(根,tblName){
var fundPositionList=root.xpath(“/Component/FundPositions/TableData/Item”);
var rowCount=fundPositionList.length();
如果(行计数!=0){
tblName.bodyRowCount=行计数;
用于(计数器=0;计数器
Build.xml

<?xml version="1.0" encoding="UTF-8" ?>
<Component ID="645248" asmid="1" def="Sheet" type="Document">
<FundPositions>
<TableData>
<Item>
<RowRank>9</RowRank>
<Port_ID>201</Port_ID>
<RowType>asset_group total</RowType>
<AssetGroup>Test1</AssetGroup>
<AssetDetail>N/A</AssetDetail>
<ActiveSecurity>28.38%</ActiveSecurity>
<DerivativeOverlay>-13.34%</DerivativeOverlay>
<NetMarketExposure>15.04%</NetMarketExposure>
<StrategicAllocation>35%</StrategicAllocation>
<FlexibleOverlay>+/- 35%</FlexibleOverlay>
<NetRange>0% to 70%</NetRange>
</Item>
<Item>
<RowRank>9</RowRank>
<Port_ID>201</Port_ID>
<RowType>asset_group total</RowType>
<AssetGroup>Test2</AssetGroup>
<AssetDetail>N/A</AssetDetail>
<ActiveSecurity>29.17%</ActiveSecurity>
<DerivativeOverlay>-1.36%</DerivativeOverlay>
<NetMarketExposure>27.80%</NetMarketExposure>
<StrategicAllocation>30%</StrategicAllocation>
<FlexibleOverlay>+/- 30%</FlexibleOverlay>
<NetRange>0% to 60%</NetRange>
</Item>
<Item>
<RowRank>2</RowRank>
<Port_ID>201</Port_ID>
<RowType>asset_group total</RowType>
<AssetGroup>Test3</AssetGroup>
<AssetDetail>N/A</AssetDetail>
<ActiveSecurity>14.72%</ActiveSecurity>
<DerivativeOverlay>0.00%</DerivativeOverlay>
<NetMarketExposure>14.72%</NetMarketExposure>
<StrategicAllocation>15%</StrategicAllocation>
<FlexibleOverlay>+/- 20%</FlexibleOverlay>
<NetRange>-5% to 35%</NetRange>
</Item>
<Item>
<RowRank>2</RowRank>
<Port_ID>201</Port_ID>
<RowType>asset_group total</RowType>
<AssetGroup>Test4</AssetGroup>
<AssetDetail>N/A</AssetDetail>
<ActiveSecurity>17.15%</ActiveSecurity>
<DerivativeOverlay>0.00%</DerivativeOverlay>
<NetMarketExposure>17.15%</NetMarketExposure>
<StrategicAllocation>15%</StrategicAllocation>
<FlexibleOverlay>+/- 20%</FlexibleOverlay>
<NetRange>-5% to 35%</NetRange>
</Item>
<Item>
<RowRank>3</RowRank>
<Port_ID>201</Port_ID>
<RowType>asset_group total</RowType>
<AssetGroup>Test5</AssetGroup>
<AssetDetail>N/A</AssetDetail>
<ActiveSecurity>5.34%</ActiveSecurity>
<DerivativeOverlay>0.00%</DerivativeOverlay>
<NetMarketExposure>5.34%</NetMarketExposure>
<StrategicAllocation>5%</StrategicAllocation>
<FlexibleOverlay>+/- 15%</FlexibleOverlay>
<NetRange>-10% to 20%</NetRange>
</Item>
<Item>
<RowRank>1</RowRank>
<Port_ID>201</Port_ID>
<RowType>detail</RowType>
<AssetGroup>Test6</AssetGroup>
<AssetDetail>Cash &amp; Cash Equivalents</AssetDetail>
<ActiveSecurity>18.24%</ActiveSecurity>
<DerivativeOverlay>0.91%</DerivativeOverlay>
<NetMarketExposure>19.15%</NetMarketExposure>
<StrategicAllocation>N/A</StrategicAllocation>
<FlexibleOverlay>N/A</FlexibleOverlay>
<NetRange>N/A</NetRange>
</Item>
<Item>
<RowRank>2</RowRank>
<Port_ID>201</Port_ID>
<RowType>asset_group total</RowType>
<AssetGroup>Test7</AssetGroup>
<AssetDetail>N/A</AssetDetail>
<ActiveSecurity>-16.25%</ActiveSecurity>
<DerivativeOverlay>17.06%</DerivativeOverlay>
<NetMarketExposure>0.80%</NetMarketExposure>
<StrategicAllocation>N/A</StrategicAllocation>
<FlexibleOverlay>N/A</FlexibleOverlay>
<NetRange>N/A</NetRange>
</Item>
</TableData>
<FundPositions_Summary>Net Market exposure summary</FundPositions_Summary>
<FundPositions_Total>100.00%</FundPositions_Total>
</FundPositions>
</Component>

9
201
资产组合计
测试1
不适用
28.38%
-13.34%
15.04%
35%
+/- 35%
0%至70%
9
201
资产组合计
测试2
不适用
29.17%
-1.36%
27.80%
30%
+/- 30%
0%至60%
2.
201
资产组合计
测试3
不适用
14.72%
0.00%
14.72%
15%
+/- 20%
-5%至35%
2.
201
资产组合计
测试4
不适用
17.15%
0.00%
17.15%
15%
+/- 20%
-5%至35%
3.
201
资产组合计
测试5
不适用
5.34%
0.00%
5.34%
5%
+/- 15%
-10%至20%
1.
201
细节
测试6
现金及;现金等价物
18.24%
0.91%
19.15%
不适用
不适用
不适用
2.
201
资产组合计
测试7
不适用
-16.25%
17.06%
0.80%
不适用
不适用
不适用
净市场敞口摘要
100.00%

考虑到百分比是以-xx%、-xx.xx%、xx%、xx.xx%的形式书写的,并且您的文档统计了两种名为红色和绿色的对象样式,下面是一种方法:

var main=function(){
var doc=app.properties.activeDocument;
如果(!doc)返回;
var green=doc.objectStyles.itemByName(“绿色”);
var red=doc.objectStyles.itemByName(“红色”);
如果(!green.isValid | |!red.isValid){
警报(“缺少绿色A/o红色对象样式。请创建这些样式并重新运行。”);
返回;
}
var fgp=app.findgrepreferences.properties;
app.findgrepreferences=null;
app.findgrepreferences.properties={
查找内容:“^-?\\d+(\\.\\d+)??%$”,
}
var found=doc.findGrep();
var n=找到的长度;
如果(!n){
警报(“抱歉,未找到%s值”);
返回;
}
变量para,isPositive,pCell,cellWidth,cellHeight,
底部、左侧、右侧、顶部、pct、矩形宽度;
var max=150;
而(n--){
para=找到[n];
pCell=para.parent;
if(单元的pCell实例){
isPositive=段落内容[0]!=“-”;
cellWidth=pCell.width;
cellHeight=pCell.height;
底部=pCell.bottomInset;
左=pCell.leftInset;
右=pCell.rightInset;
top=pCell.topInset;
cellWidth=(cellWidth左-右)*.75;
textWidth=(第endHorizontalOffset段-第horizontalOffset段)*1.1;
pct=编号(第段内容替换(/[\-%]/g,”);
rectWidth=(单元格宽度*pct)/max;
段落插入点[isPositive?0:-1]。矩形。添加({
几何边界:[0,0,单元高度上下,矩形宽度],
}).appliedObjectStyle=(isPositive?绿色:红色),
段落插入点[isPositive?1:-1]。内容=”;
}
}
app.findgrepreferences.properties=fgp;
}
var u;
app.doScript(“main()”,u,u,UndoModes.property_SCRIPT,“脚本”)<