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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…