google-apps-script - 在谷歌文档中插入日期时间

标签 google-apps-script timestamp google-docs

我想在 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/

相关文章:

javascript - 将一行从一个 Google 表格电子表格复制到另一个表格提交

python - 使用 SQL 和 python 将时间单位添加到时间戳

sqlite - sqlite触发器会触发其他触发器吗?

sql - 如何计算连续事件之间的最小时间段?

javascript - 在特定位置附加段落

google-apps-script - 使用另一个脚本更新谷歌工作表脚本

javascript - Google Apps 脚本 setValues() 不正确的高度错误

javascript - 通过 MailApp.sendEmail() 发送时,HTML 正文不会显示在电子邮件中

RGoogleDocs - uploadDoc 不会替换同名的文档

google-apps-script - 如何删除手动换行符