Spire.Office for JavaScript 11.7.0 现已正式发布。该版本支持将 Word 直接保存为 XLSX 格式,以及操作格式修订信息。同时,还支持设置图表数据标签位置,并支持追加、修改或删除 SmartArt 图形。更多详情如下。
新功能:
- 支持将 Word 直接保存为 XLSX 格式。
- 支持操作格式修订信息。
- 支持设置“定义网格时,自动调整为右缩进”。
- Paragraph 类新增 AppendSmartArt 方法,支持在段落中追加 SmartArt 图形。
- 支持隐藏表格行。
- 支持“为尾部空格添加下划线”。
- 支持读取图表数据。
- 支持获取文档修订信息。
- 支持文档兼容性选项设置。
- 支持设置“为字体调整字间距”。
- 支持设置文字方向。
- 支持提取 SmartArt 相关信息。
- 支持修订视图切换对比前后样式变化。
- 支持图表数据标签位置设置。
- 支持修改 SmartArt。
- 支持删除 SmartArt。
let doc = new spiredoc.Document();
doc.LoadFromFile(inputFileName);
doc.SaveToFile({fileName: outFileName, fileFormat: spiredoc.FileFormat.XLSX});
spire.copyFileFromFSToLocalStorage(outFileName, outputFile);
doc.Close();
let doc = new spiredoc.Document();
doc.LoadFromFile(inputFileName);
let revisionInfoCollection = doc.GetRevisionInfos();
for (let i = 0; i < revisionInfoCollection.Count; i++) {
let revisionInfo = revisionInfoCollection.get_Item(i);
if (revisionInfo.RevisionType === spiredoc.RevisionType.FormatChange)
{
revisionInfo.Reject(); // 或 Accept()
i--;
}
}
doc.SaveToFile({fileName: outFileName, fileFormat: spiredoc.FileFormat.Docx});
spire.copyFileFromFSToLocalStorage(outFileName, outputFile);
doc.Close();
let doc = new spiredoc.Document();
doc.LoadFromFile(inputFileName);
let section = doc.Sections.get_Item(0);
let para1 = section.Paragraphs.get_Item(2);
para1.Format.AdjustRightIndent = true;
section.AddParagraph();
let para3 = section.Paragraphs.get_Item(7);
para3.AppendText("News Line Sample");
para3.Format.AdjustRightIndent = true;
doc.SaveToFile({fileName: outFileName, fileFormat: spiredoc.FileFormat.Docx});
spire.copyFileFromFSToLocalStorage(outFileName, outputFile);
doc.Close();
let document = new spiredoc.Document();
let section = document.AddSection();
let paragraph = section.AddParagraph();
paragraph.Format.HorizontalAlignment = spiredoc.HorizontalAlignment.Center;
let textRange = paragraph.AppendText("RepeatingBendingProcess");
textRange.CharacterFormat.FontSize = 28;
textRange.CharacterFormat.FontName = "Times New Roman";
paragraph = section.AddParagraph();
paragraph = section.AddParagraph();
paragraph.Format.HorizontalAlignment = spiredoc.HorizontalAlignment.Center;
let shape = paragraph.AppendSmartArt(spiredoc.SmartArtType.RepeatingBendingProcess, 432, 252);
let repeatingBendingSmartArt = shape.SmartArt;
let process1 = repeatingBendingSmartArt.Nodes.get_Item(0);
process1.Text = "1";
if (process1.Paragraphs.get_Item(0).ChildObjects.get_Item(0) instanceof spiredoc.TextRange) {
process1.Paragraphs.get_Item(0).ChildObjects.get_Item(0).CharacterFormat.FontName = "Calibri";
process1.Paragraphs.get_Item(0).ChildObjects.get_Item(0).CharacterFormat.FontSize = 20;
process1.Paragraphs.get_Item(0).ChildObjects.get_Item(0).CharacterFormat.TextColor = spiredoc.Color.FromArgb(220, 20, 60);
}
repeatingBendingSmartArt.UpdateSmartArt();
document.SaveToFile({fileName: outFileName, fileFormat: spiredoc.FileFormat.Docx});
spire.copyFileFromFSToLocalStorage(outFileName, outputFile);
document.Close();
let doc = new spiredoc.Document();
doc.LoadFromFile(inputFileName);
let table = doc.FirstSection.Body.Tables.get_Item(0);
let firstRow = table.FirstRow;
firstRow.Hidden = true;
// 验证行内所有元素的 CharacterFormat.Hidden
for (let c = 0; c < row.Cells.Count; c++) {
let cell = row.Cells.get_Item(c);
for (let p = 0; p < cell.Paragraphs.Count; p++) {
let para = cell.Paragraphs.get_Item(p);
for (let r = 0; r < para.ChildObjects.Count; r++) {
let run = para.ChildObjects.get_Item(r);
if (run instanceof spiredoc.TextRange) { ... }
}
}
}
doc.SaveToFile({fileName: outFileName, fileFormat: spiredoc.FileFormat.Docx});
spire.copyFileFromFSToLocalStorage(outFileName, outputFile);
doc.Close();
let doc = new spiredoc.Document();
doc.CompatibilityOptions.UlTrailSpace = true; // 或 false
let sec = doc.AddSection();
let para = sec.AddParagraph();
let blanks = "(6) ";
let tr = para.AppendText(blanks);
tr.CharacterFormat.UnderlineStyle = spiredoc.UnderlineStyle.Single;
doc.SaveToFile({fileName: outFileName, fileFormat: spiredoc.FileFormat.Docx2013});
spire.copyFileFromFSToLocalStorage(outFileName, outputFile);
let doc = new spiredoc.Document();
doc.LoadFromFile(inputFileName);
for (let s = 0; s < doc.Sections.Count; s++) {
let sec = doc.Sections.get_Item(s);
for (let p = 0; p < sec.Paragraphs.Count; p++) {
let paragraph = sec.Paragraphs.get_Item(p);
for (let i = 0; i < paragraph.ChildObjects.Count; i++) {
let obj = paragraph.ChildObjects.get_Item(i);
if (obj instanceof spiredoc.ShapeObject) {
let shape = obj;
let chart = shape.Chart;
// 读取X轴数据
for (let x = 0; x < chart.XValues.Count; x++) {
let xVal = chart.XValues.get_Item(x);
}
// 读取Y轴数据
let series = chart.Series.get_Item(0);
for (let y = 0; y < series.YValues.Count; y++) {
let yVal = series.YValues.get_Item(y);
}
}
}
}
}
doc.Dispose();
let doc = new spiredoc.Document();
doc.LoadFromFile(inputFileName);
let revisionInfoCollection = doc.GetRevisionInfos();
for (let i = 0; i < revisionInfoCollection.Count; i++) {
let revisionInfo = revisionInfoCollection.get_Item(i);
// 获取修订信息
let author = revisionInfo.Author;
let revisionType = revisionInfo.RevisionType;
let dateTime = revisionInfo.DateTime;
let ownerObjType = revisionInfo.OwnerObject.DocumentObjectType;
// 判断作用对象类型
if (revisionInfo.OwnerObject instanceof spiredoc.TextRange) {
let range = revisionInfo.OwnerObject;
}
}
doc.Dispose();
let doc = new spiredoc.Document();
// 单独设置兼容选项
doc.CompatibilityOptions.UlTrailSpace = false;
doc.CompatibilityOptions.AdjustLineHeightInTable = true;
doc.CompatibilityOptions.SpaceForUL = true;
doc.CompatibilityOptions.ApplyBreakingRules = true;
doc.CompatibilityOptions.DoNotExpandShiftReturn = false;
doc.CompatibilityOptions.OverrideTableStyleFontSizeAndJustification = false;
doc.CompatibilityOptions.DoNotAutofitConstrainedTables = true;
// 按Word版本优化
doc.CompatibilityOptions.OptimizeForWordVersion(spiredoc.WordVersion.Word2016);
doc.SaveToFile({fileName: outFileName, fileFormat: spiredoc.FileFormat.Docx2016});
spire.copyFileFromFSToLocalStorage(outFileName, outputFile);
doc.Close();
let doc = new spiredoc.Document();
let sec = doc.AddSection();
for (let item of testData) {
let pa = sec.AddParagraph();
let textRange = pa.AppendText(item.text);
textRange.CharacterFormat.Kerning = item.kerning;
}
doc.SaveToFile({fileName: outFileName, fileFormat: spiredoc.FileFormat.Docx});
spire.copyFileFromFSToLocalStorage(outFileName, outputFile);
doc.Close();
let doc = new spiredoc.Document();
let section = doc.AddSection();
section.TextDirection = spiredoc.TextDirection.RightToLeft;
let paragraph = section.AddParagraph();
let range = paragraph.AppendText("一、選擇題:每題");
let farEastLayout2 = paragraph.AppendText("34");
let style = new spiredoc.FarEastLayout();
style.Vertical = true;
farEastLayout2.CharacterFormat.FarEastLayout = style;
doc.SaveToFile({fileName: outFileName, fileFormat: spiredoc.FileFormat.Doc});
spire.copyFileFromFSToLocalStorage(outFileName, outputFile);
doc.Close();
let document = new spiredoc.Document();
document.LoadFromFile(inputFileName);
for (let s = 0; s < document.Sections.Count; s++) {
let section = document.Sections.get_Item(s);
for (let p = 0; p < section.Paragraphs.Count; p++) {
let paragraph = section.Paragraphs.get_Item(p);
for (let c = 0; c < paragraph.ChildObjects.Count; c++) {
let childObj = paragraph.ChildObjects.get_Item(c);
if (childObj instanceof spiredoc.Shape && childObj.HasSmartArt) {
let smartArt = childObj.SmartArt;
// 获取SmartArt类型
let type = smartArt.SmartArtType;
// 获取背景填充
let bgFillType = smartArt.BackgroundFill.FillType;
// 遍历节点
for (let n = 0; n < smartArt.Nodes.Count; n++) {
let node = smartArt.Nodes.get_Item(n);
// 节点文本、字体、形状属性
node.Text;
node.ShapeProperties.get(0).Fill.FillType;
node.ShapeProperties.get(0).Fill.Color;
node.ShapeProperties.get(0).LineFormat.Fill.FillType;
}
}
}
}
}
document.Dispose();
let doc = new spiredoc.Document();
doc.LoadFromFile(inputFileName);
let revisionInfoCollection = doc.GetRevisionInfos();
for (let i = 0; i < revisionInfoCollection.Count; i++) {
let revisionInfo = revisionInfoCollection.get_Item(i);
if (revisionInfo.RevisionType === spiredoc.RevisionType.FormatChange) {
if (revisionInfo.OwnerObject instanceof spiredoc.TextRange) {
let range = revisionInfo.OwnerObject;
doc.RevisionsView = spiredoc.RevisionsView.Original;
// 读取Original样式
let bold = range.CharacterFormat.Bold;
doc.RevisionsView = spiredoc.RevisionsView.Final;
// 读取Final样式
let color = range.CharacterFormat.TextColor;
}
}
}
doc.Close();
let doc = new spiredoc.Document();
for (let pos of positions) {
let section = doc.AddSection();
let newPara = section.AddParagraph();
let shape = newPara.AppendChart(spiredoc.ChartType.Pie, 500, 300);
let chart = shape.Chart;
chart.Series.get_Item(0).HasDataLabels = true;
chart.Series.get_Item(0).DataLabels.ShowCategoryName = true;
chart.Series.get_Item(0).DataLabels.ShowValue = true;
chart.Series.get_Item(0).DataLabels.Position = pos;
}
doc.SaveToFile({fileName: outFileName, fileFormat: spiredoc.FileFormat.Docx});
spire.copyFileFromFSToLocalStorage(outFileName, outputFile);
doc.Dispose();
let document = new spiredoc.Document();
document.LoadFromFile(inputFileName);
let paragraph = document.LastParagraph;
let shape1 = paragraph.ChildObjects.get_Item(0);
let smartArt = shape1.SmartArt;
// 修改背景填充
smartArt.BackgroundFill.FillType = spiredoc.FillType.Solid;
smartArt.BackgroundFill.Color = spiredoc.Color.FromArgb(255, 242, 169, 132);
// 修改节点文本
let node = smartArt.Nodes.get_Item(0);
node.Text = "Goals";
// 修改节点填充
let shape = node.ShapeProperties.get(0);
shape.Fill.FillType = spiredoc.FillType.Solid;
shape.Fill.Color = spiredoc.Color.FromArgb(255, 160, 43, 147);
shape.LineFormat.Fill.FillType = spiredoc.FillType.Solid;
shape.LineFormat.Fill.Color = spiredoc.Color.FromArgb(255, 160, 43, 147);
// 添加新节点
let newNode = smartArt.Nodes.Add();
newNode.Text = "地图";
smartArt.UpdateSmartArt();
document.SaveToFile({fileName: outFileName, fileFormat: spiredoc.FileFormat.Docx});
spire.copyFileFromFSToLocalStorage(outFileName, fullOutputPath);
document.Close();
let document = new spiredoc.Document();
document.LoadFromFile(inputFileName);
for (let j = 0; j < document.Sections.get_Item(0).Paragraphs.Count; j++) {
let paragraph = document.Sections.get_Item(0).Paragraphs.get_Item(j);
for (let i = 0; i < paragraph.ChildObjects.Count; i++) {
let childObj = paragraph.ChildObjects.get_Item(i);
try {
let smartArt = childObj.SmartArt;
if (smartArt != null) {
paragraph.Items.RemoveAt(i);
i--;
}
} catch (e) { }
}
}
document.SaveToFile({fileName: outFileName, fileFormat: spiredoc.FileFormat.Docx});
spire.copyFileFromFSToLocalStorage(outFileName, fullOutputPath);
document.Close();
获取 Spire.Office for JavaScript 11.7.0,请点击:
https://www.e-iceblue.cn/Downloads/Spire-Office-Javascript.html






