如何在 ONLYOFFICE 演示文稿中调整段落首行缩进

2025年05月12日作者:Krystal

在制作演示文稿时,保持内容的一致性与可读性至关重要,而段落首行缩进作为格式设置的关键环节,直接影响着整体呈现效果。在本文中,我们将介绍如何通过创建 ONLYOFFICE 宏,快速设置演示文稿中所有段落的首行缩进。

如何在 ONLYOFFICE 演示文稿中调整段落首行缩进

构建宏

设置缩进值

创建宏的第一步是确定每个段落首行的缩进值:

 const indentationValue = 720; 
/* Please enter the paragraph's first line indentation value. A value of 0 means no indentation,
 and any value greater than 0 will indent the text. The value is specified in twentieths of a point (1/1440 of an inch).*/

indentationValue:此常量用于定义段落首行的缩进距离,单位为“1点”的二十分之一(即1/1440英寸,约为1.27厘米)。它的默认值为720,相当于首行缩进0.5英寸(约1.27厘米)。如果该值为0,则代表不缩进;若该值为任意大于0的数,则段落首行会缩进。用户可根据个人偏好自由调整段落缩进值。

获取演示文稿和幻灯片数量

在获取当前演示文稿之前,我们应当先确保宏只在用户输入的缩进值是有效且非负的数字时执行:

if (!isNaN(indentationValue) && indentationValue >= 0) {

接下来,使用 GetPresentation() 方法获取当前演示文稿,并通过 GetSlidesCount() 方法获取幻灯片总数:

        let presentation = Api.GetPresentation();
        let nSlides = presentation.GetSlidesCount(); // Get the number of slides in the presentation

循环遍历幻灯片、文本框和段落

    // Loop through each slide
        for (let slideIndex = 0; slideIndex < nSlides; slideIndex++) {
            let slide = presentation.GetSlideByIndex(slideIndex); // Get the slide
            let aShapes = slide.GetAllShapes(); // Get all shapes in the slide

            // Loop through each shape in the slide
            for (let shapeIndex = 0; shapeIndex < aShapes.length; shapeIndex++) {
                let content = aShapes[shapeIndex].GetDocContent(); // Get the document content of the shape
            
                if (content) {
                    let count = content.GetElementsCount(); // Get the number of elements (paragraphs) in the shape
            
                    // Loop through each paragraph in the shape
                    for (let elementIndex = 0; elementIndex < count; elementIndex++) {
                        let paragraph = content.GetElement(elementIndex); // Get the paragraph element
  • GetSlideByIndex(slideIndex):访问演示文稿中特定索引位置的幻灯片对象。
  • GetAllShapes():获取当前幻灯片中的所有图形对象,包括文本框、图片及其他元素。
  • GetDocContent():获取特定图形中的文档内容,此方法会返回与图形关联的文本内容。
  • GetElementsCount():获取特定图形中元素(段落)的总数。
  • GetElement(elementIndex):根据提供的索引(elementIndex)访问图形中某个特定元素(段落)

在这部分,我们进行了以下操作:

  1. 循环遍历演示文稿中的所有幻灯片。
  2. 循环遍历幻灯片中的所有图形(文本框)。
  3. 获取图形中的文本内容。
  4. 检查图形是否包含文本内容。
  5. 循环遍历图形中每个元素(段落)。
  6. 获取段落元素。

调整段落首行缩进

我们使用 GetParaPr() 来获取段落属性,使用 SetIndFirstLine(indentationValue) 来调整首行缩进,最后能够设置想要的段落首行缩进值:

                  let paraPr = paragraph.GetParaPr();
                        paraPr.SetIndFirstLine(indentationValue);
                    }
                }
            }
        }
    }

完整宏代码

完整的宏代码如下所示:

(function () {
    const indentationValue = 720; 
/* Please enter the paragraph's first line indentation value. A value of 0 means no indentation, 
and any value greater than 0 will indent the text. The value is specified in twentieths of a point (1/1440 of an inch).*/

    if (!isNaN(indentationValue) && indentationValue >= 0) {
        
        let presentation = Api.GetPresentation();
        let nSlides = presentation.GetSlidesCount(); // Get the number of slides in the presentation
        
        // Loop through each slide
        for (let slideIndex = 0; slideIndex < nSlides; slideIndex++) {
            let slide = presentation.GetSlideByIndex(slideIndex); // Get the slide
            let aShapes = slide.GetAllShapes(); // Get all shapes in the slide

            // Loop through each shape in the slide
            for (let shapeIndex = 0; shapeIndex < aShapes.length; shapeIndex++) {
                let content = aShapes[shapeIndex].GetDocContent(); // Get the document content of the shape
            
                if (content) {
                    let count = content.GetElementsCount(); // Get the number of elements (paragraphs) in the shape
            
                    // Loop through each paragraph in the shape
                    for (let elementIndex = 0; elementIndex < count; elementIndex++) {
                        let paragraph = content.GetElement(elementIndex); // Get the paragraph element
                        let paraPr = paragraph.GetParaPr();
                        paraPr.SetIndFirstLine(indentationValue);
                    }
                }
            }
        }
    }
})();

这个宏提供了在演示文档中给所有段落加上首行缩进的有效方法。它不仅能帮您节省时间,还能确保幻灯片整体风格的一致性。通过自动化处理首行缩进,你可以轻松完成精致、专业的排版布局,并将更多精力用于内容创作上。

我们鼓励您探索 ONLYOFFICE API 方法,创建您自己的宏来进一步优化工作流程。如果您有任何想法或建议,请随时联系我们。期待得到您的反馈!

关于作者

How to adjust paragraph first line indentation in ONLYOFFICE presentations

Marija Vujanac:我是一名前端开发者,拥有土木工程背景,并且热爱解决逻辑难题。在从事工程师工作多年,并通过图库摄影发挥创意之后,我找到了真正热爱的事情,那就是通过编程来构建事物。我喜欢使用 JavaScript、React 和 Firebase 等技术来打造用户友好的网页体验。在进行编程时,我常常沉浸其中,忘记了时间——我认为这是个好迹象!我始终在寻找新的成长方式,并希望能为有意义的项目作出贡献。

创建免费的 ONLYOFFICE 账户

在线查看并协作编辑文本文档、电子表格、幻灯片、表单和 PDF 文件。