我想在Google文档中插入时间戳(日期和/或时间)。支持文档()表示应该有一个键盘快捷键,但它在我的环境中不起作用(Win7 + IE9)。
任何人都可以为我提供Google Apps脚本来实现这一目标吗?
这很好用
在Google文档中:工具 - >打开脚本编辑器并保存此脚本
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脚本类别,bug Text Factory提供了插入时间戳的功能。
这是一个编辑版本(已经提供的版本),打印出包含时间的日期戳。
以下是输出的示例:美国东部时间2月23日21:21 26-03-2014
function onOpen() {
var ui = DocumentApp.getUi();
// Or FormApp or SpreadsheetApp.
ui.createMenu('Insert Date')
.addItem('Insert Date', 'insertDate')
.addToUi();
}
function insertTime() {
var d = new Date();
var timeStamp = d.getTime(); // Number of ms since Jan 1, 1970
// OR:
var currentTime = d.toLocaleTimeString(); // "12:35 PM", for instance
}
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 timeStamp = d.getTime(); // Number of ms since Jan 1, 1970
var currentTime = d.toLocaleTimeString(); // "12:35 PM", for instance
var date = currentTime + " " + 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;
}
对于Docs,你可能运气不好,因为似乎有no hotkey for that,并且缺乏对Docs内部脚本的支持(Spreadsheets将是一个不同的故事)。因为你在Windows上,你可以利用autohotkey。 This video,虽然太长,显示分配一个全局热键组合,以在任何地方插入当前日期。这样,您可以在使用Windows系统时在任何地方插入日期/时间。 (您可以将其自定义为仅在某些应用程序中处于活动状态,例如IE,如果您想搞砸它)
因为使用额外的击键来从菜单中插入日期,我的解决方案是批处理文件sts.cmd,它在调用时将日期时间存储到剪贴板中,从而实现简单的Windows + R,sts,按Ctrl + V进行获取和粘贴。如果您要添加额外的击键来将日期输入到文档中,您也可以将其粘贴进去。代码是
@ECHO OFF
for /f "tokens=1-12 delims=/:. " %%d in ("%time%") do SET MYTIME= %%d:%%e:%%f
for /f "tokens=1-12 delims=/:. " %%d in ("%date%") do SET MYDATE= %%g-%%e-%%f
SET MYTS=%MYDATE%%MYTIME%
ECHO | SET /p dummyname=%MYTS%|clip
这对我有用,直到GDocs提出了一个可嵌入的函数,它将在每次打开文档时将显示日期更新为当前日期。
在MacOS上,我使用了Automator。
我创建了一个与Chrome配合使用的服务,不接受任何输入,并运行shell脚本并将输出发送到剪贴板。 shell脚本非常基本:
DateTime=`date "+%Y-%m-%d %H:%M"`
echo $DateTime
我可以在没有中间变量的情况下完成并只运行date命令,但我想我可能想要进行额外的处理和格式化。请注意,如果要在日期字符串的格式中包含空格,则格式参数必须使用引号。屏幕截图使用下划线。
保存脚本后,我进入系统设置>键盘>快捷方式,我在文本部分找到了我的脚本DateTime2CB.workflow并给它一个热键。没有简单的方法可以找出正在使用的热键。
我可以为任何应用程序制作它(如果我发现自己试图使用其他应用程序,我仍然可以这样做),但现在它只是Chrome。我也可以将shell脚本输出日期输出到剪贴板,在Mac上使用pbcopy,如上所述(使用“clip”)。我测试了两种方法,但它们都有效。
我将相同的脚本添加到另一台Mac,并设置为坚持将Automator文件(name.workflow)保存在iCloud中。在该上下文中,它未在键盘设置中列出。为了解决这个问题,我不得不使用Open> Recent在Automator中重新打开保存的iCloud文件。它询问我是否要在服务中安装该文件。那时的答案是肯定的,将其保存在我的本地系统上,我可以在设置>键盘> Shorcuts>服务下找到它。
我希望这有助于其他Mac用户。
八月
如果要在打开文档后自动获取当前日期,可以添加以下脚本:
在Google文档中:工具 - >打开脚本编辑器并保存此脚本:
/**
* After open document actualize part with text "Generated" to "Generated [actual date]".
*/
function onOpen() {
var body = DocumentApp.getActiveDocument().getBody();
var date = Utilities.formatDate(new Date(), "GMT", "dd.MM.yyyy");
// Clear the text surrounding "Apps Script", with or without text.
body.replaceText("^Generated.*$", "Generated " + date);
}
在文档正文中,您必须具有“生成”文本。
试试这个:
function insertTimestamp() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var cell = ss.getActiveCell();
cell.setValue(new Date());
// sets the cells format to month/day/year.
// Remove if you want time inserted as well
cell.setNumberFormat("MM/dd/yyyy")
}
这适用于电子表格,不确定您是否正在寻找其他文档。
今天在谷歌文档中创建一个电子表格。将日期放在B1中作为今天()使用函数文本(B1,“dddd,mmmm d,yyyy”)将其格式化为C1然后使用以下脚本(您将需要今日电子表格的URL)
function myFunction() {
var doc = DocumentApp.getActiveDocument();
var body1 = doc.getBody();
var style1 = {};
style1[DocumentApp.Attribute.BOLD] = true;
var text1 = doc.editAsText();
body1.appendHorizontalRule();
var wb = SpreadsheetApp.openByUrl('spreadsheet url');
var ss = wb.getSheetByName('Today');
var r = ss.getRange('C1');
var date1 = r.getValues();
var par1 =body1.appendParagraph(date1[0]);
par1.setAttributes(style1);
}