我想在 Google 文档中插入时间戳(日期和/或时间)。支持文档()说应该有键盘快捷键,但是在我的环境(Win7+IE9)下不行。
谁能为我提供一个 Google Apps 脚本来实现这一目标?
最佳答案
这很好用
在 Google Docs 中:工具 -> 打开脚本编辑器并保存此脚本
function onOpen() {
var ui = DocumentApp.getUi();
// Or FormApp or SpreadsheetApp.
ui.createMenu('Custom Menu')
.addItem('Insert Date', 'insertDate')
.addToUi();
}
function insertDate() {
var cursor = DocumentApp.getActiveDocument().getCursor();
if (cursor) {
// Attempt to insert text at the cursor position. If insertion returns null,
// then the cursor's containing element doesn't allow text insertions.
var d = new Date();
var dd = d.getDate();
dd = pad(dd, 2)
var mm = d.getMonth() + 1; //Months are zero based
mm = pad(mm, 2)
var yyyy = d.getFullYear();
var date = dd + "-" + mm + "-" + yyyy;
var element = cursor.insertText(date);
if (element) {
element.setBold(true);
} else {
DocumentApp.getUi().alert('Cannot insert text at this cursor location.');
}
} else {
DocumentApp.getUi().alert('Cannot find a cursor in the document.');
}
}
function pad (str, max) {
str = str.toString();
return str.length < max ? pad("0" + str, max) : str;
}
重新加载文档,接受权限。
关于google-apps-script - 在谷歌文档中插入日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13585272/