如何使用脚本在 Google 文档中插入和居中文本
How to insert and center text in Google Docs with script
我想制作一个脚本,在我的文档末尾插入这样的文本:
February 18, 2015
Title
Entry
前两行居中,最后一行用制表符缩进。
这是我目前拥有的:
function addJournalTemplate(){
var currDate = new Date();
var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var dateString = months[currDate.getMonth()] + ' ' + (currDate.getDate() < 10 ? '0' + currDate.getDate() : currDate.getDate()) + ', ' + currDate.getYear();
var doc = DocumentApp.getActiveDocument();
var dateText = doc.getBody().editAsText().appendText(dateString);
var title = doc.getBody().insertParagraph(1, 'Title');
title.setAlignment(DocumentApp.HorizontalAlignment.CENTER);
}
而且该代码根本不起作用。我迷路了,现在已经搜索 developers.google.com 大约一个小时了。
我该怎么做?
我修改了你的脚本。
你的概率:
var dateText = doc.getBody().editAsText().appendText(dateString);
你不能用这个方法居中,最好用方法( insertParagraph() )
function addJournalTemplate(){
var currDate = new Date();
var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var dateString = months[currDate.getMonth()] + ' ' + (currDate.getDate() < 10 ? '0' + currDate.getDate() : currDate.getDate()) + ', ' + currDate.getYear();
var doc = DocumentApp.openById('myid');
var dateText = doc.getBody().insertParagraph(1,dateString);
dateText.setAlignment(DocumentApp.HorizontalAlignment.CENTER);
var title = doc.getBody().insertParagraph(2, 'Title');
title.setAlignment(DocumentApp.HorizontalAlignment.CENTER);
return doc;
}
我想制作一个脚本,在我的文档末尾插入这样的文本:
February 18, 2015
Title
Entry
前两行居中,最后一行用制表符缩进。
这是我目前拥有的:
function addJournalTemplate(){
var currDate = new Date();
var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var dateString = months[currDate.getMonth()] + ' ' + (currDate.getDate() < 10 ? '0' + currDate.getDate() : currDate.getDate()) + ', ' + currDate.getYear();
var doc = DocumentApp.getActiveDocument();
var dateText = doc.getBody().editAsText().appendText(dateString);
var title = doc.getBody().insertParagraph(1, 'Title');
title.setAlignment(DocumentApp.HorizontalAlignment.CENTER);
}
而且该代码根本不起作用。我迷路了,现在已经搜索 developers.google.com 大约一个小时了。
我该怎么做?
我修改了你的脚本。
你的概率:
var dateText = doc.getBody().editAsText().appendText(dateString);
你不能用这个方法居中,最好用方法( insertParagraph() )
function addJournalTemplate(){
var currDate = new Date();
var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var dateString = months[currDate.getMonth()] + ' ' + (currDate.getDate() < 10 ? '0' + currDate.getDate() : currDate.getDate()) + ', ' + currDate.getYear();
var doc = DocumentApp.openById('myid');
var dateText = doc.getBody().insertParagraph(1,dateString);
dateText.setAlignment(DocumentApp.HorizontalAlignment.CENTER);
var title = doc.getBody().insertParagraph(2, 'Title');
title.setAlignment(DocumentApp.HorizontalAlignment.CENTER);
return doc;
}