Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
304 views
in Technique[技术] by (71.8m points)

google apps script - onEdit multiple functions specific cell

I am trying to get multiple functions to fire at the same time when editing one cell.

I have tried two different ways but it doesn't work. When I run the first option without the cell reference it is working but as soon as I try to make it dependent on the one cell it stops working.

Option 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()); 
}

Option 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()); 
}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Do not define a function within a function

Just get rid of function AllData() and instead simply call

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

Mind that since all your funcitons a revery similar, you can make our code much easier by calling the same funciton with different parameters.

Sample:

//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()); 
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...