Skip to content

Instantly share code, notes, and snippets.

@moluapple
Created July 11, 2012 07:47
Show Gist options
  • Save moluapple/3088804 to your computer and use it in GitHub Desktop.
Save moluapple/3088804 to your computer and use it in GitHub Desktop.
[Indesign] 单元格小数点居中对齐
/***************************************************
* 确保单元格文本对齐方式为左对齐,通过以下方式选择需要对其的单元格:
* 1. 仅选中首行数据列(也即非表头及文字列)单元格运行,将自动对齐直至末行的所有数据
* 2. 直接选中所有单元格(如2行2列至100行5列)运行,对齐所有选中的单元格
* 可自动判别对齐小数点或分号(不含空格)
***************************************************/
(function() {
var oColumns = app.selection[0].columns,
i = 0,
rCount = app.selection[0].rows.length,
cCount = app.selection[0].columns.length,
name, lastCell;
for (; i < oColumns.length; i++) {
name = app.selection[0].cells[i].name.split(':');
lastCell = rCount > 1 ? oColumns[i].cells.item(name[0] + ':' + (Number(name[1]) + rCount - 1)) : oColumns[i].cells[-1];
alignMe(oColumns[i].cells.itemByRange(app.selection[0].cells[i], lastCell));
}
function alignMe(oCells) {
var tabPosition, str = oCells.paragraphs.everyItem().contents.join('@'),
alignChar = str.replace(/\d+|[,@\s]/g, '')[0] ? '.' : ',',
sortFunc = function(a, b) {
return a.length - b.length
},
regex = alignChar == '.' ? '\\.' : ',',
left = str.replace(RegExp(regex + '\\d+', 'g'), '').split('@').sort(sortFunc).pop(),
right = str.replace(RegExp('(@\\d+' + regex + '?|^\\d+' + regex + '?)', 'g'), '@').split('@').sort(sortFunc).pop(),
firstP = oCells.paragraphs[0],
originContents = firstP.contents.toString(),
originOffset = firstP.horizontalOffset;
firstP.contents = left + alignChar + right;
firstP.justification = Justification.CENTER_ALIGN;
desiredOffset = firstP.characters[left.length].horizontalOffset;
firstP.justification = Justification.LEFT_ALIGN;
firstP.texts[0].contents = originContents;
tabPosition = desiredOffset - originOffset;
oCells.paragraphs.everyItem().tabStops.add({
alignment: TabStopAlignment.CHARACTER_ALIGN,
alignmentCharacter: alignChar,
position: tabPosition
})
}
})();
/***************************************************
* 通过以下方式选择需要对其的单元格,运行脚本:
* 1. 仅选中首行数据列(也即非表头及文字列)单元格运行,将自动对齐直至末行的所有数据
* 2. 直接选中所有单元格(如2行2列至100行5列)运行,对齐所有选中的单元格
***************************************************/
(function() {
var oColumns = app.selection[0].columns,
i = 0,
rCount = app.selection[0].rows.length,
cCount = app.selection[0].columns.length,
name, lastCell;
for (; i < oColumns.length; i++) {
name = app.selection[0].cells[i].name.split(':');
lastCell = rCount > 1 ? oColumns[i].cells.item(name[0] + ':' + (Number(name[1]) + rCount - 1)) : oColumns[i].cells[-1];
alignMe(oColumns[i].cells.itemByRange(app.selection[0].cells[i], lastCell));
}
function alignMe(oCells) {
oCells.paragraphs.everyItem().justification = Justification.CENTER_ALIGN;
var offsets = oCells.paragraphs.everyItem().endHorizontalOffset;
maxOffset = Math.max.apply(null, offsets);
oCells.paragraphs.everyItem().justification = Justification.RIGHT_ALIGN;
var newOffset = oCells.paragraphs[0].endHorizontalOffset;
var nIndent = newOffset - maxOffset;
oCells.paragraphs.everyItem().rightIndent = nIndent;
}
})();
@ednykissyou
Copy link

您好,用过这两个脚本,很棒很棒。
只是有时因调整需要,会调将部分列拉宽或缩榨,这时再对这一列应用脚本就没有效果。
我看了一下,那是因为用过一次之后就会有右缩进值。
能否在代码最前加上一个右缩归0的代码,然后再运行上述代码即完美了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment