使用Google Apps脚本从Google文档中删除空白的最后一页空间,制表符 [英] Removing empty last page space, tab from Google document using Google Apps Script
本文介绍了使用Google Apps脚本从Google文档中删除空白的最后一页空间,制表符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用Google Apps脚本从Google文档中删除最后一页空白的制表符
您好,我是脚本新手
我的文档通常在复制/粘贴后包含几页带空格和制表符的页面
我正在尝试擦除输入打印的最后一个字符之后的所有内容,而不必选择页数
我只需要打印第一页
你能给我带头吗?
tanaikech脚本不适合我的使用...太糟糕了
推荐答案
Tanaike's script仅删除最后一个段落,但不检查该段落是否为空。
如果您想删除文档末尾的所有空段,可以这样做(注意,它会为每个空段调用API,请参见我的更新版本):
function main() {
var doc = DocumentApp.getActiveDocument();
var id = doc.getId();
var text = doc.getBody().getText();
var pgfs = text.split('
');
while(pgfs.pop().replace(/s+/,"") == "") remove_last_pgf(id);
}
// original Tanaike's script goes here
function remove_last_pgf(docId) {
var c = Docs.Documents.get(docId, {
fields: "body.content"
}).body.content.pop();
Docs.Documents.batchUpdate(
{
requests: [
{
deleteContentRange: {
range: { startIndex: c.startIndex - 1, endIndex: c.endIndex - 1 }
}
}
]
},
docId
);
}
别忘了在脚本编辑器的服务中添加Google Docs API:
更新
我对脚本做了进一步的改进:
function main() {
var doc = DocumentApp.getActiveDocument();
var docId = doc.getId();
var empty_tail = doc.getBody().getText().search(/s+$/) + 1;
if (empty_tail == 0) return; // prevent the error for empty docs
var content = Docs.Documents.get(docId,{fields: "body.content"}).body.content.pop();
var range = { startIndex: empty_tail, endIndex: content.endIndex-1 };
var req = { deleteContentRange: { range } };
Docs.Documents.batchUpdate( {requests: [req] }, docId );
}
现在它应该工作得更快了,因为它不会为每一行空行调用API。它获取空字符s+
开始的位置,并通过一次API调用将它们全部移除。
这篇关于使用Google Apps脚本从Google文档中删除空白的最后一页空间,制表符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文