如何创建触发器以并行运行多个功能(传递参数)

时间:2019-07-16 09:03:38

标签: multithreading google-apps-script google-sheets triggers

我有一个函数,我希望多次运行以减少对该函数的大修处理:

function ModifyVAxisChartThreading(){
  var nbChart=39;
   i++;
   for(var j=i;j<nbChart;j+=4)
  {
    Vmax=rangeMax[j*6];
    Vmin=rangeMinId[j*6];
    id=rangeMinId[(j*6)+1];
    var delta=(Vmax-Vmin)*0.1;
    Logger.log("Vmax="+Vmax+"Vmin="+Vmin+"id="+id+"i="+j);
    var currChart = chart[id];
    if(currChart.getType()!="COLUMN")
    {
      Vmin-=delta
      Vmax=Number(Vmax)+(delta*1.5)//Number() function to avoid Vmax becoming a string for no reason
    }
    Logger.log("Vmax="+Vmax+"Vmin="+Vmin+"id="+id+"j="+j);
     currChart = currChart.modify()
      .setOption('vAxes', {0: {textStyle: {fontSize: 10}, titleTextStyle: {fontSize : 8}, viewWindow: {min: Vmin, max:Vmax}}})//adpative vaxis for AREA and COMBO
      .build();
    sheet.updateChart(currChart);
  }

}

我已经制作了一个函数test(),该函数创建了两个触发器,但是触发器仅运行该函数 ModifyVAxisChartThreading() ,其基值i相同 (0)。

function test(){
  //delete all previous trigger
  var allTriggers = ScriptApp.getProjectTriggers();
  for (var i = 0; i < allTriggers.length; i++) {
      ScriptApp.deleteTrigger(allTriggers[i]);

  }
 i=-1;
  var ss=SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Dashboard Data");
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Dashboard");
 rangeMax =ss.getRange("O3:O231").getValues();//Range to modify if you add charts
 rangeMinId=ss.getRange("P3:P232").getValues();//Range to modify if you add charts
 chart = sheet.getCharts();
 var nbChart=19;
  Logger.log("range Max ="+rangeMax + "autre="+rangeMinId);
 //create the trigger to run the function ModifyVAxisChartThreading in parallel
 for(var j=0;j<4;j++)
  {
  Utilities.sleep(1000);
  var testTrigger =ScriptApp.newTrigger("ModifyVAxisChartThreading")
  .timeBased()
  .after((1) * 60 * 1000)
  .create();
  Logger.log(testTrigger.getUniqueId());
  }
}

这是我的全局变量:

var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Dashboard");
var ss=SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Dashboard Data");
var rangeMax =ss.getRange("O3:O231").getValues();//Range to modify if you add charts
var rangeMinId=ss.getRange("P3:P232").getValues();//Range to modify if you add charts
var i=-1;
var  chart = sheet.getCharts();

1 个答案:

答案 0 :(得分:1)

好的,所以我找到了为我的函数运行多个线程的解决方案。这是:

var RECURRING_KEY = "recurring";//variable for 
var ARGUMENTS_KEY = "arguments";//passing argument to trigger
var nbThread =2;//nb of trigger for the function for one sheet

function ModifyVAxisChartThreading(e){
  var scriptProperties = PropertiesService.getScriptProperties();
  var triggerId = e.triggerUid;
  var triggerData = JSON.parse(scriptProperties.getProperty(e.triggerUid));
  var sheetName=triggerData[ARGUMENTS_KEY];//get the string argument of the trigger
  var ss=SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName+"Dashboard Data");
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName+"Dashboard");
  var nbChart=39;
  var rangeMax =ss.getRange("O3:O231").getValues();//Range to modify if you add charts
  var rangeMinId=ss.getRange("P3:P232").getValues();//Range to modify if you add charts
  var chart = sheet.getCharts();
  var Vmax=0; var Vmin=0;var id=-1;var delta=0;
  for(var j=triggerData[RECURRING_KEY]/*get the int argument*/;j<nbChart;j+=nbThread)
  {
    Vmax=rangeMax[j*6];
    Vmin=rangeMinId[j*6];
    id=rangeMinId[(j*6)+1];
    delta=(Vmax-Vmin)*0.1;
    Logger.log("Vmax="+Vmax+"Vmin="+Vmin+"id="+id+"i="+j);
    var currChart = chart[id];
    if(currChart.getType()!="COLUMN")
    {
      Vmin-=delta
      Vmax=Number(Vmax)+(delta*1.5)//Number() function to avoid Vmax becoming a string for no reason
    }
    Logger.log("Vmax="+Vmax+"Vmin="+Vmin+"id="+id+"j="+j);
     currChart = currChart.modify()
      .setOption('vAxes', {0: {textStyle: {fontSize: 10}, titleTextStyle: {fontSize : 8}, viewWindow: {min: Vmin, max:Vmax}}})//adpative vaxis for AREA and COMBO
      .build();
    sheet.updateChart(currChart);
  }
}

//This is the function that create all the triggers to parallelize the function MofifyVAxisChartThreading() 
function test(){
  //delete all previous trigger
  var allTriggers = ScriptApp.getProjectTriggers();
  for (var i = 0; i < allTriggers.length; i++) {
      ScriptApp.deleteTrigger(allTriggers[i]);

  }
  var sheetNames=["sheet0 ","sheet1 ","sheet2 ","sheet3 ","sheet4 ","sheet5 ","sheet6 ","sheet7 ","sheet8 "];
 //create the trigger to run the function ModifyVAxisChartThreading in parallel
 for(var z=0;z<sheetNames.length;z++)//create nbThread triggers for every sheets
 {
   for(var j=0;j<nbThread;j++)
   {
     Utilities.sleep(1000);
     var testTrigger =ScriptApp.newTrigger("ModifyVAxisChartThreading")
     .timeBased()
     .after((0.15) * 60 * 1000)
     .create();
     Logger.log(testTrigger.getUniqueId());
     var triggerData = {};
     triggerData["i"] = j ;
     setupTriggerArguments(testTrigger, sheetNames[z], j);//pass the parameters to the trigger
   }
 }
}
//this function pass the parameters in the properties of the trigger
function setupTriggerArguments(trigger, functionArguments, recurring) {
  var triggerUid = trigger.getUniqueId();
  var triggerData = {};
  triggerData[RECURRING_KEY] = recurring;
  triggerData[ARGUMENTS_KEY] = functionArguments;
  PropertiesService.getScriptProperties().setProperty(triggerUid, JSON.stringify(triggerData));
}

此链接中的代码极大地启发了传递参数的代码:How can I pass a parameter to a time-based Google App Script trigger?