2017-08-02 67 views
1

我是新來的JavaScript或谷歌應用腳​​本。我正在使用此功能從谷歌電子表格中將數據導入MySQL數據庫。這張表有超過16000條記錄,我想將導入細分爲2000個小記錄。在函數createGASTrigger()中,我將如何更改邏輯,以便在第一次迭代中從電子表格中獲取第一個2000條記錄,然後在第二次迭代中獲取下一個2000條記錄,依此類推。谷歌應用程序腳本使用觸​​發器重新執行功能

功能1

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 myFunction() { 

    var stime = new Date(); 
    var col1; 
    var col2; 
    var col3; 

    var dbconnection = Jdbc.getConnection(dbUrl, root, rootPwd); 
    var statement = dbconnection.createStatement(); 
    var googlesheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('sheet1'); 
    var data = googlesheet.getDataRange().getValues(); 

dbconnection.setAutoCommit(false) 

for (var i = 1; i < data.length; i++) { 
col1 = data[i][0]; 
col2 = data[i][1]; 
col3 = data[i][2]; 

var sql = "{call [dbo].[sp_googlesheetimport](?,?,?)}"; 
statement = dbconnection.prepareCall(sql); 
statement.setString(1, col1); 
statement.setString(2, col2); 
statement.setString(3, col3); 
statement.addBatch(); 
} 

statement.executeBatch(); 
dbconnection.commit(); 
statement.close(); 
dbconnection.close(); 

    var etime = new Date(); 
    Logger.log('Exec time: ' + (etime.getTime() - stime.getTime())); 
} 

功能2

function createGASTrigger() { 
    var varUserPropertiesService = PropertiesService.getUserProperties(); 
    var varUserTriggerId = varUserPropertiesService.getProperty("myFunction"); 
    var varUserTrigger = ScriptApp.getProjectTriggers(); 

    for (var i in varUserTrigger) { 
     if (varUserTrigger[i].getUniqueId() == varUserTriggerId) 
     try 
     { 
      ScriptApp.deleteTrigger(varUserTrigger[i]); 
     } 
     catch(e) 
     { 
      Utilities.sleep(30000); 
      ScriptApp.deleteTrigger(varUserTrigger[i]); 
     } 
    } 

    myFunction(); 

    var userProperties = PropertiesService.getUserProperties(); 
    try 
    { 
     var nextTrigger = ScriptApp.newTrigger("myFunction").timeBased().after(1 * 120 * 1000).create(); 
    } 
    catch(e) 
    { 
     Utilities.sleep(30000); 
     var nextTrigger = ScriptApp.newTrigger("myFunction").timeBased().after(1 * 120 * 1000).create(); 
    }  
} 

回答

2

我沒有測試過這個,但我認爲這是一個很好的起點噸。

function myFunction() { 

    var stime = new Date(); 
    var col1; 
    var col2; 
    var col3; 
    var dbconnection = Jdbc.getConnection(dbUrl, root, rootPwd); 
    var statement = dbconnection.createStatement(); 
    var googlesheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('sheet1'); 
    var data = googlesheet.getDataRange().getValues(); 
    var itrows=1000; 
    var complete=false; 
    if(PropertiesService.getScriptProperties().getProperty('iteration')) 
    { 
    var iteration=Number(PropertiesService.getScriptProperties().getProperty('iteration') + 1); 
    } 
    else 
    { 
    var iteration=Number(PropertiesService.getScriptProperties().setProperty('iteration', 1)); 
    } 

    var max=itrows * iteration; 
    var min=max - itrows + 1; 
    if(max>data.length-1) 
    { 
    max=data.length-1; 
    complete=true; 
    } 


    dbconnection.setAutoCommit(false) 

    for (var i=min;i<=max;i++) 
    { 
    col1 = data[i][0]; 
    col2 = data[i][1]; 
    col3 = data[i][2]; 
    var sql = "{call [dbo].[sp_googlesheetimport](?,?,?)}"; 
    statement = dbconnection.prepareCall(sql); 
    statement.setString(1, col1); 
    statement.setString(2, col2); 
    statement.setString(3, col3); 
    statement.addBatch(); 
    } 

    statement.executeBatch(); 
    dbconnection.commit(); 
    statement.close(); 
    dbconnection.close(); 
    if(complete) 
    { 
    PropertiesService.getScriptProperties().deleteProperty('iteration'); 
    } 
    var etime = new Date(); 
    Logger.log('Exec time: ' + (etime.getTime() - stime.getTime())); 
} 

function createGASTrigger() { 
    var varUserPropertiesService = PropertiesService.getUserProperties(); 
    var varUserTriggerId = varUserPropertiesService.getProperty("myFunction"); 
    var varUserTrigger = ScriptApp.getProjectTriggers(); 

    for (var i=0;i<varUserTrigger.length;i++) 
    { 
     if (varUserTrigger[i].getUniqueId() == varUserTriggerId) 
     { 
     ScriptApp.deleteTrigger(varUserTrigger[i]); 
     break; 
     } 
    } 
    var userProperties = PropertiesService.getUserProperties(); 
    var nextTrigger = ScriptApp.newTrigger("myFunction").timeBased().after(1 * 120 * 1000).create(); 
} 
+0

非常感謝。 – PVB

0

無論是存儲頁值PropertiesService.getScriptProperties()或值添加到電子表格中的所有行,你將它們添加

相關問題