在Google文档中插入日期时间

rbr*_*egt 36 google-apps-script

我想在Google文档中插入时间戳(日期和/或时间).支持文档()表示应该有一个键盘快捷键,但它在我的环境中不起作用(Win7 + IE9).

任何人都可以为我提供Google Apps脚本来实现这一目标吗?

Rya*_*ner 40

这很好用

在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;
}
Run Code Online (Sandbox Code Playgroud)

重新加载文档,接受权限.


ors*_*iro 9

我不确定加载项是否属于您要求的Google Apps脚本类别,错误文本工厂提供了插入时间戳的功能.

截图

  • Text Factory加载项需要在Google Apps中拥有过多的权限。我不会在我的Google Apps帐户中允许此操作。 (2认同)

小智 5

这是一个编辑版本(已经提供的版本),打印出包含时间的日期戳.

以下是输出示例:美国东部时间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;
}
Run Code Online (Sandbox Code Playgroud)


小智 5

如果要在打开文档后自动获取当前日期,可以添加以下脚本:

在 Google Docs 中:工具 -> 打开脚本编辑器并保存此脚本:

/** 
 *  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);    
}
Run Code Online (Sandbox Code Playgroud)

在文档正文中,您必须有文本“生成”。


小智 5

创建新的表格文档

在A1中,输入Date:B1,输入公式NOW()。我将工作表命名为current-date,但您可以将其命名为任何您想要的名称。该文档将简单地位于您的服务器驱动器上,作为所有文档文档的“日期管理员”。

现在新工作表()

将单元格格式设置为日期

选择单元格 B1 并Number >> DateFormat菜单中进行选择。

格式化日期

选择单元格并复制

选择单元格 A1 和 B1,右键单击,然后选择Copy

选择并复制

粘贴并链接到 Docs 文档

右键单击文档中要粘贴单元格的位置,然后单击Paste。文档应该询问您是否要将这些单元格链接到源文档。选择Link to spreadsheet然后单击Paste

粘贴和链接

摆脱边界

要消除边框,请右键单击插入的单元格并选择Table Properties

表属性

现在设置Table Border0pt并单击OK

零边界

最后结果

你最终应该得到这样的结果。您可以拖动单元格的边缘以使其变大或变小,还可以更改单元格的字体和文本大小。如果您将文本变大,文本将包裹在单元格内,因此您需要将它们加宽。

最后结果

更新日期

现在,每当您打开包含链接单元格的文档并且日期已更改时,您都应该看到这一点。单击Update,您的日期将更新为今天的日期。无需打开current-date电子表格!

更新单元格

享受!


Bri*_*nry 3

对于文档,您可能不走运,因为似乎没有热键,并且缺乏对文档内脚本的支持(电子表格将是一个不同的故事)。不过,由于您使用的是 Windows,因此您可以利用autohotkey该视频虽然太长,但显示了分配全局热键组合以在任意位置插入当前日期。这样,您就可以在使用 Windows 系统时在任何地方插入日期/时间。(如果你想搞点古怪的话,你可以将其自定义为仅在某些应用程序中激活,例如 IE)