Google apps脚本使用触​​发器重新执行功能

时间:2017-08-02 17:38:12

标签: javascript google-apps-script

我是javascript或google apps脚本的新手。我正在使用此功能从谷歌电子表格导入数据到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 个答案:

答案 0 :(得分:2)

我没有对此进行测试,但我认为这是一个很好的起点。

     var adfsUrl = null;
     var req = new XMLHttpRequest();
     req.open('GET', url + '/XrmServices/2011/Organization.svc?wsdl=wsdl0', true);
     req.setRequestHeader('Connection', 'Keep-Alive');
     req.setRequestHeader('Content-Type', 'application/soap+xml; charset=UTF-8');
     req.onreadystatechange = function () {
         if (req.readyState === 4) {
            if (req.status === 200) {
                adfsUrl = $(req.response).find('ms-xrm\\:Identifier');
            }
         }
     };
     return $(adfsUrl[0]).text().replace("http://", "https://");

答案 1 :(得分:0)

将页面值存储在PropertiesService.getScriptProperties()中,或者在添加时为所有行的电子表格添加值