首页 > 解决方案 > 如何以编程方式控制指南(第三规则)?

问题描述

有什么方法可以控制 Google 幻灯片演示文稿中的指南(第三规则)。据我阅读 Google Apps 脚本文档,没有方法可以控制指南。

我已经尝试过类似于指南(第三规则)的线条,但这些不是“真正的”指南。用户可以删除这些行,这些行仅限于幻灯片页面区域。

查看图片以了解线条和指南之间的区别(第三规则):

线条和指南的区别

要启用指南(第三规则),请访问此处:

指南

标签: google-apps-scriptgoogle-slides-apigoogle-slides

解决方案


允许通过SlidesApp或 Slides API 控制指南的功能请求已提交给问题跟踪器。如果您发现该功能有用,请加注星标。

简短的回答

如前所述,不幸的是,这目前无法通过SlidesApp服务或Slides API实现。

解决方法

实际上可以通过传递一个负整数作为insertLine. 至于能够删除线条,如果用户将指南拖出幻灯片边界,也可以“删除”指南。

我无法模仿的指南的唯一属性是在呈现时隐藏它们(因为隐藏了真正的指南)。

以下是类似于您的方法的解决方法(创建Lines 网格):

指南

/**
 * @summary emulates adding guides to the Slide
 * 
 * @param {{
 *  rows : (number|1),
 *  deleteOld : (boolean|true),
 *  cols : (number|1),
 *  color : string
 * }} 
 */
const showGuides = ({ deleteOld = true, rows = 1, cols = 1, color = "#d3d3d3" } = {}) => {

    const presentation = SlidesApp.getActivePresentation();
    const currPage = presentation.getSelection().getCurrentPage();

    const prop = "guides";

    const store = PropertiesService.getUserProperties();

    /** @type {string[]} */
    let guideRefs = JSON.parse(store.getProperty(prop) || "[]");

    const lines = currPage.getLines();

    const oldLines = lines.filter((line) => guideRefs.includes(line.getObjectId()));

    if (deleteOld) {
        oldLines.forEach(line => line.remove());

        guideRefs = removeElements(guideRefs, oldLines.map(l => l.getObjectId()));
    }

    const currWidth = presentation.getPageWidth();
    const currHeight = presentation.getPageHeight();

    const xStep = Math.floor(currWidth / cols);
    const yStep = Math.floor(currHeight / rows);

    const newGuides = [];

    const maxScreen = 4096;

    for (let x = 0; x <= cols; x++) {
        const line = currPage.insertLine(
            SlidesApp.LineCategory.STRAIGHT,
            xStep * x, -maxScreen, xStep * x, currHeight + maxScreen
        );

        const fill = line.getLineFill();
        fill.setSolidFill(color);

        const oid = line.getObjectId();
        guideRefs.push(oid);
        newGuides.push(line);
    }

    for (let y = 0; y <= rows; y++) {
        const line = currPage.insertLine(
            SlidesApp.LineCategory.STRAIGHT,
            -maxScreen, yStep * y, currWidth + maxScreen, yStep * y
        );

        const fill = line.getLineFill();
        fill.setSolidFill(color);

        const oid = line.getObjectId();
        guideRefs.push(oid);
        newGuides.push(line);
    }

    store.setProperty(prop, JSON.stringify(guideRefs));
};

实用程序

/**
 * @summary removes elements from an array
 * @param {any[]} arr 
 * @param {...any} elems
 * @returns {any[]}
 */
const removeElements = (arr, ...elems) => arr.filter((elem) => !elems.includes(elem));

演示

变通方法测试


推荐阅读