onEdit несколько функций с указанием c ячейки - PullRequest
0 голосов
/ 05 августа 2020

Я пытаюсь заставить несколько функций запускаться одновременно при редактировании одной ячейки.

Я пробовал два разных способа, но это не сработало. Когда я запускаю первый вариант без ссылки на ячейку, он работает, но как только я пытаюсь сделать его зависимым от одной ячейки, он перестает работать.

Вариант 1.

function onEdit(e)
{
  if (e.range.getA1Notation() === '') {
    
    function AllData(){
      importData1(); 
      SpreadsheetApp.flush();
      importData2(); 
      SpreadsheetApp.flush();
      importData3();
    }
  }
}
var sourceSpreadsheetID = "XXX";
var sourceWorksheetName = "Sheet1";
var targetSpreadsheetID = "yyy";
var targetWorksheetName = "Sheet 2";

function importData1() {
  
  var thisSpreadsheet = SpreadsheetApp.openById(sourceSpreadsheetID);
  var thisWorksheet = thisSpreadsheet.getSheetByName(sourceWorksheetName);
  var thisData = thisWorksheet.getDataRange();
  var thisData = thisSpreadsheet.getRangeByName("data1");
  
  var toSpreadsheet = SpreadsheetApp.openById(targetSpreadsheetID);
  var toWorksheet = toSpreadsheet.getSheetByName(targetWorksheetName);
  var toRange = toWorksheet.getRange(9, 2, thisData.getNumRows(), thisData.getNumColumns())
  toRange.setValues(thisData.getValues()); 
}


var sourceSpreadsheetID = "XXX";
var sourceWorksheetName = "Sheet1";
var targetSpreadsheetID = "yyy";
var targetWorksheetName = "Sheet 2";

function importData2() {
  
  var thisSpreadsheet = SpreadsheetApp.openById(sourceSpreadsheetID);
  var thisWorksheet = thisSpreadsheet.getSheetByName(sourceWorksheetName);
  var thisData = thisWorksheet.getDataRange();
  var thisData = thisSpreadsheet.getRangeByName("data2");
  
  var toSpreadsheet = SpreadsheetApp.openById(targetSpreadsheetID);
  var toWorksheet = toSpreadsheet.getSheetByName(targetWorksheetName);
  var toRange = toWorksheet.getRange(9, 7, thisData.getNumRows(), thisData.getNumColumns())
  toRange.setValues(thisData.getValues()); 
}

var sourceSpreadsheetID = "XXX";
var sourceWorksheetName = "Sheet1";
var targetSpreadsheetID = "yyy";
var targetWorksheetName = "Sheet 2";

function importData3() {
  
  var thisSpreadsheet = SpreadsheetApp.openById(sourceSpreadsheetID);
  var thisWorksheet = thisSpreadsheet.getSheetByName(sourceWorksheetName);
  var thisData = thisWorksheet.getDataRange();
  var thisData = thisSpreadsheet.getRangeByName("data3");
  
  var toSpreadsheet = SpreadsheetApp.openById(targetSpreadsheetID);
  var toWorksheet = toSpreadsheet.getSheetByName(targetWorksheetName);
  var toRange = toWorksheet.getRange(9, 11, thisData.getNumRows(), thisData.getNumColumns())
  toRange.setValues(thisData.getValues()); 
}

Вариант 2.

function createOnEditTrigger() {
 var ss = SpreadsheetApp.openById(targetSpreadsheetID);
 ScriptApp.newTrigger("importData")
   .forSpreadsheet(ss)
   .onEdit()
   .create();
}

var sourceSpreadsheetID = "1qu_AheZoX6Z4H1GF2yBwvlFxLlCkQhmuYfg-fP8z2kc";
var sourceWorksheetName = "tankers-tool-database";
var targetSpreadsheetID = "1ozefsBT-LBmWXPI4QqDG4BSAzfmY_Yqp2q_sXTevhpk";
var targetWorksheetName = "tankers-search-db";

function importData(e) {
 if (e.range.getA1Notation() === "A1") {

        function AllData(){
       importData1(); 
       SpreadsheetApp.flush();
       importData2(); 
       SpreadsheetApp.flush();
       importData3();
}
 }
}

function importData1() {
   
 var thisSpreadsheet = SpreadsheetApp.openById(sourceSpreadsheetID);
 var thisWorksheet = thisSpreadsheet.getSheetByName(sourceWorksheetName);
 var thisData = thisWorksheet.getDataRange();
 //Uncomment line 11 below and comment out line 9 if you want to sync a named range. Replace "teamBugs" with your named range.
 var thisData = thisSpreadsheet.getRangeByName("data1");

 var toSpreadsheet = SpreadsheetApp.openById(targetSpreadsheetID);
 var toWorksheet = toSpreadsheet.getSheetByName(targetWorksheetName);
 var toRange = toWorksheet.getRange(9, 2, thisData.getNumRows(), thisData.getNumColumns())
 toRange.setValues(thisData.getValues()); 
}



function importData2() {
   
 var thisSpreadsheet = SpreadsheetApp.openById(sourceSpreadsheetID);
 var thisWorksheet = thisSpreadsheet.getSheetByName(sourceWorksheetName);
 var thisData = thisWorksheet.getDataRange();
 //Uncomment line 11 below and comment out line 9 if you want to sync a named range. Replace "teamBugs" with your named range.
 var thisData = thisSpreadsheet.getRangeByName("data2");

 var toSpreadsheet = SpreadsheetApp.openById(targetSpreadsheetID);
 var toWorksheet = toSpreadsheet.getSheetByName(targetWorksheetName);
 var toRange = toWorksheet.getRange(9, 7, thisData.getNumRows(), thisData.getNumColumns())
 toRange.setValues(thisData.getValues()); 
}



function importData3() {
   
 var thisSpreadsheet = SpreadsheetApp.openById(sourceSpreadsheetID);
 var thisWorksheet = thisSpreadsheet.getSheetByName(sourceWorksheetName);
 var thisData = thisWorksheet.getDataRange();
 //Uncomment line 11 below and comment out line 9 if you want to sync a named range. Replace "teamBugs" with your named range.
 var thisData = thisSpreadsheet.getRangeByName("data3");

 var toSpreadsheet = SpreadsheetApp.openById(targetSpreadsheetID);
 var toWorksheet = toSpreadsheet.getSheetByName(targetWorksheetName);
 var toRange = toWorksheet.getRange(9, 11, thisData.getNumRows(), thisData.getNumColumns())
 toRange.setValues(thisData.getValues()); 
}

1 Ответ

0 голосов
/ 05 августа 2020

Не определяйте функцию внутри функции

Просто избавьтесь от function AllData() и вместо этого просто вызовите

function importData(e) {
  if (e.range.getA1Notation() === "A1") {    
      importData1(); 
      SpreadsheetApp.flush();
      importData2(); 
      SpreadsheetApp.flush();
      importData3();
    }
}

Помните, что, поскольку все ваши функции похожи друг на друга, вы можете сделать наш код намного проще, вызывая одну и ту же функцию с разными параметрами.

Пример:

//global variables
var sourceSpreadsheetID = "1qu_AheZoX6Z4H1GF2yBwvlFxLlCkQhmuYfg-fP8z2kc";
var sourceWorksheetName = "tankers-tool-database";
var targetSpreadsheetID = "1ozefsBT-LBmWXPI4QqDG4BSAzfmY_Yqp2q_sXTevhpk";
var targetWorksheetName = "tankers-search-db";
var thisSpreadsheet = SpreadsheetApp.openById(sourceSpreadsheetID);
var thisWorksheet = thisSpreadsheet.getSheetByName(sourceWorksheetName);
var toSpreadsheet = SpreadsheetApp.openById(targetSpreadsheetID);
var toWorksheet = toSpreadsheet.getSheetByName(targetWorksheetName);
//assuming you built a trigger for the following function already
function importData(e) {
  if (e.range.getA1Notation() === "A1") {    
  //call the same funciton 3 times but with different parameters
    importData("data1",2); 
    SpreadsheetApp.flush();
    importData("data2", 7); 
    SpreadsheetApp.flush();
    importData("data3", 11);
  }
}

function importData(data, column) {  
  var thisData = thisSpreadsheet.getRangeByName(data);
  var toRange = toWorksheet.getRange(9, column, thisData.getNumRows(), thisData.getNumColumns())
  toRange.setValues(thisData.getValues()); 
}
...