繁体   English   中英

在脚本控制下的 Google Docs 中,插入表格后自动插入的段落可以通过编程方式解决吗?

[英]In Google Docs under script control, can a paragraph inserted automatically after the insertion of a table be addressed programmatically?

我有一个 Google Docs 文档,其中有一个 PARAGRAPH,后跟一个 TABLE,后跟一个 TABLE。 从视觉上看,两个 TABLE 之间有一个 PARAGRAPH。 但是,以编程方式,使用以下代码,运行日志表明没有 PARAGRAPH,即

[1] PARAGRAPH {'LEFT_TO_RIGHT' : true, 'LINE_SPACING' : 1.15, 'SPACING_AFTER' : 0, 'SPACING_BEFORE' : 0, 'INDENT_FIRST_LINE' : 0, 'INDENT_END' : 0, 'INDENT_START' : 0} 
[1/1] TEXT {} perth influencer
[2] TABLE {'BORDER_WIDTH' : 1, 'BORDER_COLOR' : '#000000'} 
[3] TABLE {'BORDER_WIDTH' : 1, 'BORDER_COLOR' : '#000000'} Keyword Research Volume
...

根据appendTable的 Google Apps 脚本文档:

此方法还将 append 表格后的空段落,因为 Google Docs 文档不能以表格结尾。

这一段可以用眼睛看到,但目前的脚本无法“看到”它。 也就是说,单步执行文档正文的子元素无法检测到自动插入段落的存在。 这是一个问题,因为我想减小该段落的磅值。

这可能是通过 Google Apps 脚本对 Google Docs 的已知限制。 或者它可能是我的错误代码,所以下面是我断言的函数。 他们除了报告他们的发现之外什么都不做,但即便如此,也许我错过了一些东西。

上面的 output 是通过使用GoogleAppsScript.Document.Body LogChildren编码并引用生成的文档的正文来生成的。

String.prototype.quoted = function () {
  return  "'" + this.replace(/'/g,"\\'") + "'";
}

Number.prototype.quoted = function () {
  return String(this);
}

Boolean.prototype.quoted = function () {
    return this ? "true" : "false";
}

function getInnerText(child) {
    switch (child.getType().toString()) {
        case "BODY_SECTION":
            return child.asBody().getText();
            break;
        case "EQUATION":
            return child.asEquation().getText();
            break;
        case "EQUATION_FUNCTION":
            return child.asEquationFunction().getText();
            break;
        case "FOOTER_SECTION":
            return child.asFooterSection().getText();
            break;
        case "FOOTNOTE_SECTION":
            return child.asFootnoteSection().getText();
            break;
        case "HEADER_SECTION":
            return child.asHeaderSection().getText();
            break;
        case "LIST_ITEM":
            return child.asListItem().getText();
            break;
        case "PARAGRAPH":
            return "";
            break;
        case "TABLE":
            return child.asTable().getText();
            break;
        case "TABLE_CELL":
            return child.asTableCell().getText();
            break;
        case "TABLE_OF_CONTENTS":
            return child.asTableOfContents().getText();
            break;
        case "TABLE_ROW":
            return child.asTableRow().getText();
            break;
        case "TEXT":
            return child.asText().getText();
            break;
        case "PAGE_BREAK":
            return "";
            break;
        case "INLINE_IMAGE":
            return child.asInlineImage().getLinkUrl();
            break;
        default:
            return child.asText().getText();
            break;
    }
}
function getStyles(child) {
    const attribs = child.getAttributes();
    const attribList = [];
    for (let att in attribs) {
        try {
            if (null !== attribs[att])
                attribList.push(att.quoted() + " : " + attribs[att].quoted());
        }
        catch (E) { }
    }
    return "{" + attribList.join(", ") + "}";
}
function LogChild(index, child) {
    Logger.log("[%s] %s %s %s", index, child.getType().toString(), getStyles(child), getInnerText(child));
}
function LogChildren(body) {
    function LogDeeper(cc, child) {
        const childCount = child.getNumChildren();
        for (let c = 0; c < childCount; c++) {
            LogChild(String(cc) + "/" + String(c + 1), child.getChild(c));
        }
    }
    const childCount = body.getNumChildren();
    for (let c = 0; c < childCount; c++) {
        const child = body.getChild(c);
        LogChild(String(c + 1), child);
        if (isParagraph(child)) {
            LogDeeper(c + 1, child.asParagraph());
        }
        else if (isListItem(child)) {
            LogDeeper(c + 1, child.asListItem());
        }
    }
}
function isPageBreak(elem) {
    return elem.getType() === DocumentApp.ElementType.PAGE_BREAK;
}
function isText(elem) {
    return elem.getType() === DocumentApp.ElementType.TEXT;
}
function isParagraph(elem) {
    return elem.getType() === DocumentApp.ElementType.PARAGRAPH;
}
function isListItem(elem) {
    return elem.getType() === DocumentApp.ElementType.LIST_ITEM;
}
function isTable(elem) {
    return elem.getType() === DocumentApp.ElementType.TABLE;
}

使用Document#get检索文档结构,如果两个表之间记录了中间段落,则发出UpdateParagraphStyleRequest以修改该段落。

您可以通过高级 Google 服务从应用程序脚本访问 api

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2023 STACKOOM.COM