Google Apps 脚本 setTimestamp 到 mysql 数据库
Posted
技术标签:
【中文标题】Google Apps 脚本 setTimestamp 到 mysql 数据库【英文标题】:Google Apps Script setTimestamp to mysql database 【发布时间】:2020-04-02 03:38:53 【问题描述】:我正在尝试创建一个谷歌应用程序脚本,该脚本采用 CSV 文件并将其推送到 mysql 数据库中。我无法插入日期和日期时间字段。 CSV 日期和日期时间字段的格式如下:2015-12-04 和 2015-12-04 20:02:14(如果包含时间)。我正在尝试在我的脚本中使用以下语句。 “日期”字段只有一个日期,没有时间,似乎它可能正在工作,所以问题可能是时间格式?:
var stmt = conn.prepareStatement('INSERT INTO product_detail('
+'Invoice,Sales_ID,Product,Report_Category,Menu_Price,Sales,'
+'Net_Sales,Gross_Sales,Sales_before_Discount_after_Taxes,Sales_after_Discount_after_Taxes,'
+'Gratuity,GST,GST_Inc,PST,PST_Inc,Total_Sales_Taxes,Total_Item_Discounts,'
+'Item_Discount_Name,Item_Discount_Employee,Total_Invoice_Discounts,Invoice_Discount_Name,'
+'Invoice_Discount_Employee,Table,Date,Date_Opened,Date_Closed,Day,Day_of_week,Week,Month,'
+'Year,Open_Station_ID,Open_Station,Closing_Station,Closing_Station_ID,Open_Employee,'
+'Closing_Employee) VALUES ( (?, ?, ?, ?, ?, ?, ?, ?,'
+ '?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
for (var i = 1; i < data.length; i++)
stmt.setString(1 ,data[i][0]);
stmt.setString(2 ,data[i][1]);
stmt.setString(3 ,data[i][2]);
stmt.setString(4 ,data[i][3]);
stmt.setString(5 ,data[i][4]);
stmt.setString(6 ,data[i][5]);
stmt.setString(7 ,data[i][6]);
stmt.setString(8 ,data[i][7]);
stmt.setString(9 ,data[i][8]);
stmt.setString(10 ,data[i][9]);
stmt.setString(11 ,data[i][10]);
stmt.setString(12 ,data[i][11]);
stmt.setString(13 ,data[i][12]);
stmt.setString(14 ,data[i][13]);
stmt.setString(15 ,data[i][14]);
stmt.setString(16 ,data[i][15]);
stmt.setString(17 ,data[i][16]);
stmt.setString(18 ,data[i][17]);
stmt.setString(19 ,data[i][18]);
stmt.setString(20 ,data[i][19]);
stmt.setString(21 ,data[i][20]);
stmt.setString(22 ,data[i][21]);
stmt.setString(23 ,data[i][22]);
var date_insert = new Date(data[i][23]);
var jdbc_timestamp = Jdbc.newTimestamp(date_insert.getTime());
stmt.setTimestamp(24 ,jdbc_timestamp);
date_insert = new Date(data[i][24]);
jdbc_timestamp = Jdbc.newTimestamp(date_insert.getTime());
stmt.setTimestamp(25 ,jdbc_timestamp);
date_insert = new Date(data[i][25]);
jdbc_timestamp = Jdbc.newTimestamp(date_insert.getTime());
stmt.setTimestamp(26 ,jdbc_timestamp);
stmt.setString(27 ,data[i][26]);
stmt.setString(28 ,data[i][27]);
date_insert = new Date(data[i][28]);
jdbc_timestamp = Jdbc.newTimestamp(date_insert.getTime());
stmt.setTimestamp(29 ,jdbc_timestamp);
stmt.setString(30 ,data[i][29]);
stmt.setString(31 ,data[i][30]);
stmt.setString(32 ,data[i][31]);
stmt.setString(33 ,data[i][32]);
stmt.setString(34 ,data[i][33]);
stmt.setString(35 ,data[i][34]);
stmt.setString(36 ,data[i][35]);
stmt.setString(37 ,data[i][36]);
stmt.addBatch();
【问题讨论】:
【参考方案1】:
getTime()
以毫秒为单位为您提供自 Unix 纪元以来的时间
如果您需要的是以某种方式格式化的时间戳字符串,则需要使用Utilities.formatDate(date, timeZone, format)。
示例:
var timeZone = date_insert.getTimezoneOffset()
var format = "yyyy-MM-dd HH:mm:ss"
var formattedDate = Utilities.formatDate(date_insert, timeZone, format);
【讨论】:
谢谢,我可以在更改 CSV 输出格式后将它们作为字符串插入。但这也有效。以上是关于Google Apps 脚本 setTimestamp 到 mysql 数据库的主要内容,如果未能解决你的问题,请参考以下文章
如何通过 Apps 脚本在 Google 表格中“清除格式”
使用 Google BigQuery / Apps 脚本为插入 Google 表格的数据添加时间戳