我们客户的在线商店使用opencart,出于营销目的,他们希望将opencart订单与google表单自动同步。我修改了googlesheets中的一个脚本,以提取过去一年的客户订单(基于https://www.transpacific-software.com/blog/opencart-to-google-docs-pull-and-sync-data-auto-through-scripting 以及https://gist.github.com/pradeepbheron/e6129c9fd9bc74e814d0)
sql查询是:
SELECT order_id, firstname AS first_name, lastname AS last_name,
email, date_added AS order_date, CONCAT("£", FORMAT(total,2))
order_value, payment_address_1 AS billing_address_1,
payment_address_2 AS billing_address_2, payment_city AS
billing_city, payment_postcode AS billing_postcode,
payment_country AS billing_country, items_purchased FROM
(
SELECT order_id, firstname, lastname, email, date_added,
total, payment_address_1, payment_address_2, payment_city,
payment_postcode, payment_country
FROM ocbw_order
GROUP BY order_id
) AS A
JOIN (
SELECT order_id AS product_order_id, GROUP_CONCAT(name
SEPARATOR ", ") AS items_purchased
FROM ocbw_order_product
GROUP BY product_order_id
) AS B
ON A.order_id=B.product_order_id
WHERE date_added >= DATE_SUB(NOW(),INTERVAL 1 YEAR)
AND firstname != ''
它在phpmyadmin中运行良好,但google脚本编辑器会生成一个“超过最大执行时间”错误。
看起来有7357行(从myphpadmin导出)。有没有更好的方法来编写查询?另外,我正在尝试重命名列标题,但只能执行两种操作,即:
GROUP_CONCAT(name SEPARATOR ", ") AS items_purchased
以及
CONCAT("£", FORMAT(total,2)) order_value
有什么想法吗
快速更新:2018年11月9日星期五11:54:03
1) 根据@thisguyhasstwomthumbs的请求,这里是explain表结果的屏幕截图
2) 我查看了@bcperth提到的最佳实践文档。我试图重写googlesheets脚本,但遇到了问题。
这是修改后的剧本。
function p1MySQLFetchData() {
// Change it as per your database credentials
var conn =
Jdbc.getConnection('jdbc:mysql://[dbHostIp]:3306/[dbName]',
'[dbUsername]', '[dbPassword]');
var stmt = conn.createStatement();
var start = new Date(); // Get script starting time
//change table name as per your database structure
var rs = stmt.executeQuery('[sqlQuery]');
// It sets the limit of the
// maximum nuber of rows in a ResultSet object
// Returns the currently active spreadsheet
var doc = SpreadsheetApp.getActiveSpreadsheet();
var cell = doc.getRange('a1');
var row = 0;
// Mysql table column name count.
var getCount = rs.getMetaData().getColumnCount();
// Create array to hold mysql data
var tempArray = [];
// get row and column count for later
var colCount = getCount;
// ATTEMPT TO GET ROW COUNT 1
//rs.last();
//var rowCount = rs.getRow();
//rs.beforeFirst(); // resets rs cursor
//Logger.log(RowCount);
// DOESN'T WORK! result => ReferenceError: "RowCount" is not
// defined. (line 28, file "Code")
// ATTEMPT TO GET ROW COUNT 2
//var rsCount = stmt.executeQuery('SELECT COUNT(*) AS rowcount FROM
//[sqlQuery]);
// It sets the limit of the maximum number of rows in a ResultSet
// object
//rsCount.next();
//var rowCount = rsCount.getString("rowcount");
//Logger.log(rowCount);
// DOESN'T WORK! result => 0
// Build TempArray using MySQL data
for (var i = 0; i < getCount; i++){
tempArray[0][i] = rs.getMetaData().getColumnName(i+1);
// DOESNT WORK => ERROR
// TypeError: Cannot set property "0.0" of undefined to "order_id".
// (line 39, file "Code")
//Logger.log(rs.getMetaData().getColumnName(i+1));
}
var row = 1;
while (rs.next()) {
for (var col = 0; col < rs.getMetaData().getColumnCount();
col++) {
tempArray[row][col] = rs.getString(col + 1);
//Logger.log(rs.getString(col + 1));
}
row++;
}
// BELOW DOESNT AS I CANT GET A ROW COUNT (SEE ABOVE)
// Fill Spreadsheet from tempArray
//for (var row = 0; row < rowCount; row++) {
//for (var col = 0; col < colCount; col++) {
//cell.offset(row, col).setValue(tempArray[row][col + 1]);
//}
// }
rs.close();
stmt.close();
conn.close();
var end = new Date(); // Get script ending time
Logger.log('Time elapsed: ' + (end.getTime() - start.getTime()));
// To generate script log. To view log click on View -> Logs.
}
但正如你从评论中看到的,我犯了很多错误。我不知道下一步该怎么办。
更新时间:2018年11月30日星期五15:26:14
回答下面的@tedinoz评论。
phpmyadmin生成6862个结果,查询耗时13.3074秒。
当我在googlescripteditor中运行这个脚本时,大约花了2分钟30分钟才完成,只提取了6348条记录(92.5%)。记录在2018年10月3日之后停止。
关于您的建议:
1) 我尝试运行修改后的脚本,将间隔设置为:
1个月=>我在谷歌没有结果(应该是529)
2个月=>我得到14个结果(应该是1029)
3个月=>我得到299个结果(应该是1669个)。
myphpadmin和googlescripteditor分别用了4-7秒和5-20秒
2) 你的意思是从phpmyadmin导出csv并导入到google工作表吗?我做到了,效果很好。
我注意到的另一件事是,google表单中的订单id与phpmyadmin中的不匹配。奇怪的。
暂无答案!
目前还没有任何答案,快来回答吧!