2015-06-20 52 views
0

在Google表格上; 我希望它顯示日/月/年&小時/分鐘在Google表格上插入日期和時間

當我點擊插入日期它顯示二零一五年六月二十零日21:59:4

我想讓它顯示20/06/2015 21:59

我已經嘗試了一些東西,我設法改變每月/每天,但它拋出錯誤,所以不得不重新開始。

如果任何人有更好的代碼或可以修復此代碼這將幫助很多!

function getDate() { 
    var d = new Date(); 
    return (d.getMonth()+1) + "/" + d.getDate() + "/" + d.getFullYear(); 
} 

function getTime() { 
    var d = new Date(), 
     offset = -d.getTimezoneOffset()/60, 
     h = d.getUTCHours() + offset, 
     m = d.getMinutes(), 
     s = d.getSeconds(); 
    return h + ":" + m + ":" + s; 
} 

function getDateAndTime() { 
    return getDate() + " " + getTime(); 
} 

function insertValue(theValue, format) { 
    var ss = SpreadsheetApp.getActiveSpreadsheet(), 
     theSheet = ss.getActiveSheet(), 
     theCell = theSheet.getActiveSelection(); 
    theCell.setNumberFormat(format || theCell.getNumberFormat()); 
    theCell.setValue(theValue); 
    return true; 
} 

function getValue() { 
    return SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getActiveCell().getValue(); 
} 

function getFormat() { 
    return SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getActiveCell().getNumberFormat(); 
} 

function insertDate() { 
    insertValue(getDate(), "Date"); 
} 

function insertTime() { 
    insertValue(getTime(), "Time"); 
} 

function insertDateAndTime() { 
    insertValue(getDateAndTime(), "Date time"); 
} 

function appendDate() { 
    insertValue(getValue() + " " + getDate()); 
} 

function appendTime() { 
    insertValue(getValue() + " " + getTime()); 
} 

function appendDateAndTime() { 
    insertValue(getValue() + " " + getDateAndTime()); 
} 

function onOpen() { 
    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
    var menuEntries = []; 
    menuEntries.push({name: "Insert Date", functionName: "insertDate"}); 
    menuEntries.push({name: "Insert Time", functionName: "insertTime"}); 
    menuEntries.push({name: "Insert Date and Time", functionName: "insertDateAndTime"}); 
    menuEntries.push(null); // line separator 
    menuEntries.push({name: "Append Date", functionName: "appendDate"}); 
    menuEntries.push({name: "Append Time", functionName: "appendTime"}); 
    menuEntries.push({name: "Append Date and Time", functionName: "appendDateAndTime"}); 

    ss.addMenu("Insert Date", menuEntries); 
} 

function onInstall() { onOpen(); } 

回答

0

做,只要你想這個公式工作:

= TEXT(NOW(), 「DD/MM/YYYY HH:MM」)

0

也許使用Utilities.formatDate()。 見here for more info

例如:

var formattedDate = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "dd/MM/yyyy HH:mm");