Javascript 将数据从google sheets导入MySQL表
使用GoogleApps脚本将数据从GoogleSheets导入MySQL表。我有一个巨大的数据集,可以将google sheet导入表中。但是,我遇到了超出最大执行时间的问题,除非有其他选项可以加速执行Javascript 将数据从google sheets导入MySQL表,javascript,mysql,google-apps-script,jdbc,Javascript,Mysql,Google Apps Script,Jdbc,使用GoogleApps脚本将数据从GoogleSheets导入MySQL表。我有一个巨大的数据集,可以将google sheet导入表中。但是,我遇到了超出最大执行时间的问题,除非有其他选项可以加速执行 var address = 'database_IP_address'; var rootPwd = 'root_password'; var user = 'user_name'; var userPwd = 'user_password'; var db = 'database_name'
var address = 'database_IP_address';
var rootPwd = 'root_password';
var user = 'user_name';
var userPwd = 'user_password';
var db = 'database_name';
var root = 'root';
var instanceUrl = 'jdbc:mysql://' + address;
var dbUrl = instanceUrl + '/' + db;
function googleSheetsToMySQL() {
var RecId;
var Code;
var ProductDescription;
var Price;
var dbconnection = Jdbc.getConnection(dbUrl, root, rootPwd);
var statement = dbconnection.createStatement();
var googlesheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('product');
var data = googlesheet.getDataRange().getValues();
for (var i = 1; i < data.length; i++) {
RecId = data[i][0];
Code = data[i][1];
ProductDescription = data[i][2];
Price = data[i][3];
var sql = "{call [dbo].[sp_googlesheetstotable](?,?,?,?)}";
statement = dbconnection.prepareCall(sql);
statement.setString(1, RecId);
statement.setString(2, Code);
statement.setString(3, ProductDescription);
statement.setString(4, Price);
statement.executeUpdate();
}
statement.close();
dbconnection.close();
}
var address='database_IP_address';
var rootPwd='root_password';
var user='user_name';
var userPwd='user_password';
var db='数据库名称';
变量根='根';
var instanceUrl='jdbc:mysql://'+地址;
var dbUrl=instanceUrl+'/'+db;
函数googleSheetsToMySQL(){
变量RecId;
var代码;
var产品描述;
var价格;
var dbconnection=Jdbc.getConnection(dbUrl、root、rootPwd);
var statement=dbconnection.createStatement();
var googlesheet=SpreadsheetApp.getActiveSpreadsheet().getSheetByName(“产品”);
var data=googlesheet.getDataRange().getValues();
对于(变量i=1;i
使用批处理执行
dbconnection.setAutoCommit(false)
for (var i = 1; i < data.length; i++) {
RecId = data[i][0];
Code = data[i][1];
ProductDescription = data[i][2];
Price = data[i][3];
var sql = "{call [dbo].[sp_googlesheetstotable](?,?,?,?)}";
statement = dbconnection.prepareCall(sql);
statement.setString(1, RecId);
statement.setString(2, Code);
statement.setString(3, ProductDescription);
statement.setString(4, Price);
statement.addBatch()
statement.executeBatch()
}
dbconnection.commit()
dbconnection.setAutoCommit(false)
对于(变量i=1;i
尝试查看此相关信息,了解如何使用应用程序脚本代码将数据从Google电子表格导入MySQL
现在,对于“错误超过最大执行时间”异常,请记住,对于单个脚本,只有6分钟/执行的最大执行时间。这意味着你超过了这个限制
试着检查一下如何防止Google脚本超过最大执行时间限制的技术
有关详细信息,请查看以下链接:
function googleSheetsToMySQL() {
var sheetName = 'name_of_google_sheet';
var dbAddress = 'database_ip_address';
var dbUser = 'database_user_name';
var dbPassword = 'database_user_password';
var dbName = 'database_name';
var dbTableName = 'database_table_name';
var dbURL = 'jdbc:mysql://' + dbAddress + '/' + dbName;
// Regarding the statement used by the OP, you might find something like....
//
// "INSERT INTO " + dbTableName + " (recid, code, product_description, price) VALUES (?, ?, ?, ?);";
//
// to be more practical if you're trying to implement the OP's code,
// as you are unlikely to have a stored procedure named 'sp_googlesheetstotable', or may be more
// familiar with basic queries like INSERT, UPDATE, or SELECT
var sql = "{call [dbo].[sp_googlesheetstotable](?,?,?,?)}";
// The more records/requests you load into the statement object, the longer it will take to process,
// which may mean you exceed the execution time before you can do any post processing.
//
// For example, you may want to record the last row you exported in the event the export must be halted
// prematurely. You could create a series of Triggers to re-initiate the export, picking up right where
// you left off.
//
// The other consideration is that you want your GAS memory utilization to remain as low as possible to
// keep things running smoothly and quickly, so try to strike a balance that fits the data you're
// working with.
var maxRecordsPerBatch = 1000;
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getSheetByName(sheetName);
var sheetData = sheet.getDataRange().getValues();
var dbConnection = Jdbc.getConnection(dbURL, dbUser, dbPassword);
// The following only needs to be set when you are changing the statement that needs to be prepared
// or when you need to reset the variable.
//
// For example, if you were to switch to a different sheet which may have different values, columns,
// structure, and/or target database table.
var dbStatement = dbConnection.prepareCall(sql);
var RecId;
var Code;
var ProductDescription;
var Price;
var recordCounter = 0;
var lastRow;
dbConnection.setAutoCommit(false);
for (var i = 1; i < sheetData.length; i++) {
lastRow = (i + 1 == sheetData.length ? true : false);
RecId = sheetData[i][0];
Code = sheetData[i][1];
ProductDescription = sheetData[i][2];
Price = sheetData[i][3];
dbStatement.setString(1, RecId);
dbStatement.setString(2, Code);
dbStatement.setString(3, ProductDescription);
dbStatement.setString(4, Price);
// This command takes what has been set above and adds the request to the array that will be sent
// to the database for processing.
dbStatement.addBatch();
recordCounter += 1;
if (recordCounter == maxRecordsPerBatch || lastRow)
{
try {
dbStatement.executeBatch();
}
catch(e)
{
console.log('Attempted to update TABLE `' + dbTableName + '` in DB `' + dbName + '`, but the following error was returned: ' + e);
}
if (!lastRow)
{ // Reset vars
dbStatement = dbConnection.prepareCall( sql ); // Better to reset this variable to avoid any potential "No operations allowed after statement closed" errors
recordCounter = 0;
}
}
}
dbConnection.commit();
dbConnection.close();
}
函数googleSheetsToMySQL(){
var sheetName='name_of_google_sheet';
var dbAddress='database_ip_address';
var dbUser='database_user_name';
var dbPassword='database_user_password';
var dbName='数据库名称';
var dbTableName='database_table_name';
var dbURL='jdbc:mysql://'+dbAddress+'/'+dbName;
//关于OP使用的语句,您可能会发现类似于。。。。
//
//“插入“+dbTableName+”(recid、代码、产品描述、价格)值(?、、?、?);”;
//
//为了更实用,如果你想实现OP的代码,
//因为您不太可能有一个名为“sp_googlesheetstotable”的存储过程,或者可能有更多
//熟悉插入、更新或选择等基本查询
var sql=“{call[dbo].[sp_googlesheetstotable](?,?,?,?)}”;
//加载到语句对象中的记录/请求越多,处理它所需的时间就越长,
//这可能意味着您在进行任何后期处理之前已经超过了执行时间。
//
//例如,如果必须停止导出,您可能希望记录导出的最后一行
//您可以创建一系列触发器来重新启动导出,从正确的位置开始
//你停下来了。
//
//另一个需要考虑的问题是,您希望GAS内存利用率保持尽可能低,以便
//让事情平稳、快速地运行,因此,请尝试找到一种适合您当前数据的平衡
//与……合作。
var maxRecordsPerBatch=1000;
var电子表格=SpreadsheetApp.getActiveSpreadsheet();
var sheet=电子表格.getSheetByName(sheetName);
var sheetData=sheet.getDataRange().getValues();
var dbConnection=Jdbc.getConnection(dbURL、dbUser、dbPassword);
//只有在更改需要准备的语句时,才需要设置以下内容
//或者当您需要重置变量时。
//
//例如,如果要切换到可能具有不同值、列的不同图纸,
//结构和/或目标数据库表。
var dbStatement=dbConnection.prepareCall(sql);
变量RecId;
var代码;
var产品描述;
var价格;
var-recordCounter=0;
var lastRow;
dbConnection.setAutoCommit(false);
对于(变量i=1;i<?php
require __DIR__ . '/vendor/autoload.php';
/*
* We need to get a Google_Client object first to handle auth and api calls, etc.
*/
$client = new \Google_Client();
$client->setApplicationName('My PHP App');
$client->setScopes([\Google_Service_Sheets::SPREADSHEETS]);
$client->setAccessType('offline');
/*
* The JSON auth file can be provided to the Google Client in two ways, one is as a string which is assumed to be the
* path to the json file. This is a nice way to keep the creds out of the environment.
*
* The second option is as an array. For this example I'll pull the JSON from an environment variable, decode it, and
* pass along.
*/
$jsonAuth = getenv('JSON_AUTH');
$client->setAuthConfig(json_decode($jsonAuth, true));
/*
* With the Google_Client we can get a Google_Service_Sheets service object to interact with sheets
*/
$sheets = new \Google_Service_Sheets($client);
/*
* To read data from a sheet we need the spreadsheet ID and the range of data we want to retrieve.
* Range is defined using A1 notation, see https://developers.google.com/sheets/api/guides/concepts#a1_notation
*/
$data = [];
// The first row contains the column titles, so lets start pulling data from row 2
$currentRow = 2;
// The range of A2:H will get columns A through H and all rows starting from row 2
$spreadsheetId = getenv('SPREADSHEET_ID');
$range = 'A2:H';
$rows = $sheets->spreadsheets_values->get($spreadsheetId, $range, ['majorDimension' => 'ROWS']);
if (isset($rows['values'])) {
foreach ($rows['values'] as $row) {
/*
* If first column is empty, consider it an empty row and skip (this is just for example)
*/
if (empty($row[0])) {
break;
}
$data[] = [
'col-a' => $row[0],
'col-b' => $row[1],
'col-c' => $row[2],
'col-d' => $row[3],
'col-e' => $row[4],
'col-f' => $row[5],
'col-g' => $row[6],
'col-h' => $row[7],
];
/*
* Now for each row we've seen, lets update the I column with the current date
*/
$updateRange = 'I'.$currentRow;
$updateBody = new \Google_Service_Sheets_ValueRange([
'range' => $updateRange,
'majorDimension' => 'ROWS',
'values' => ['values' => date('c')],
]);
$sheets->spreadsheets_values->update(
$spreadsheetId,
$updateRange,
$updateBody,
['valueInputOption' => 'USER_ENTERED']
);
$currentRow++;
}
}
print_r($data);
/* Output:
Array
(
[0] => Array
(
[col-a] => 123
[col-b] => test
[col-c] => user
[col-d] => test user
[col-e] => usertest
[col-f] => email@domain.com
[col-g] => yes
[col-h] => no
)
[1] => Array
(
[col-a] => 1234
[col-b] => another
[col-c] => user
[col-d] =>
[col-e] => another
[col-f] => another@eom.com
[col-g] => no
[col-h] => yes
)
)
*/