40

タイムスタンプ (日付および/または時刻) を Google ドキュメントに挿入したいと考えています。サポート ドキュメント () には、キーボード ショートカットが必要であると記載されていますが、私の環境 (Win7 + IE9) では機能しません。

これを実現するための Google Apps スクリプトを提供してくれる人はいますか?

4

10 に答える 10

43

これはうまくいきます

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;
}

ドキュメントを再読み込みし、権限を受け入れます。

于 2014-02-10T14:15:37.610 に答える
9

アドオンが、あなたが求めていた Google Apps Script のカテゴリに該当するかどうかはわかりません。バグText Factoryには、タイムスタンプを挿入する機能があります。

スクリーンショット

于 2016-11-08T17:57:26.407 に答える
5

ドキュメントを開いた後に自動的に現在の日付を取得する場合は、次のスクリプトを追加できます。

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);    
}

ドキュメントの本文には、「Generated」というテキストが必要です。

于 2018-11-20T08:10:17.453 に答える
3

For Docs, you are probably out of luck, as there appears to be no hotkey for that, and support for scripting from within Docs is lacking (Spreadsheets would be a different story). Since you're on Windows, you can avail yourself of autohotkey, though. This video, while way too long, shows assigning a global hotkey combo to insert the current date anywhere. With that, you can insert your date/time anywhere you want to while using your Windows system. (You can customize it to be only active in certain applications, such as IE, if you want to get wacky with it)

于 2012-11-27T15:11:38.023 に答える
3

メニューから日付を挿入するために余分なキーストロークが使用されているため、私の解決策はバッチファイル sts.cmd です。 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 が思いつくまで、私にとってはうまくいきます。

于 2014-09-14T19:35:25.347 に答える
-4

これを試して:

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") 
}

これは、他のドキュメントを探しているかどうかわからないスプレッドシートで機能します。

于 2013-04-30T05:24:44.457 に答える
-4

Google ドキュメントで今日のスプレッドシートを作成します。B1 に Today() として日付を入れます 関数 Text(B1,"dddd, mmmm d, yyyy") を使用して C1 で書式設定します 次に、次のスクリプトを使用します (Today スプレッドシートの 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);
}
于 2014-01-06T21:53:43.490 に答える