下面的脚本将满足您的需求。
当您在订单表中进行更改时,交货表将正确更新。它将更新:
- 打开交货单时
- 如果您按下
REFRESH
交货单上的按钮。
(所以不像使用“ query(IMPORTRANGE..)
”公式那样自动)。
这是您需要安装的代码。
function onOpen() {
updateTracker();
}
function updateTracker(){
// This function is executed when the sheet is opened
// and also intended to be linked to a REFRESH button to be installed in the sheet
// The function populates ColA to ColK with data from another sheet
// Existing data in ColL and ColM has to be preserved, and realigned
// with the same invoice numbers in ColA of the new data.
// Step 1 - read ColA, ColL and ColM of the old data, before repopulating ColA and ColsK
//---------------------------------------------------------------------------------------
//var openSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Open Order Tracker");
var openSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var lastRow = openSheet.getLastRow(); // locate last row of data
var invNoArray = openSheet.getRange(2,1,lastRow-1).getValues(); // 1D array of invoices numbers
var invNoList = {};
for(var row=0; row < invNoArray.length; row++){ // make an "associative array" of invoice numbers
invNoList[invNoArray[row]] = row;
}
// read the delivery dates and driver from this sheet ('Open Order Tracker')
var driverArray = openSheet.getRange(2,12,lastRow-1,3).getValues(); // 2D array of Delivery Dates and Drivers
// clear out the old content (not formats or vaidation)
var currentRange = openSheet.getRange(2,1,lastRow,14);
currentRange.clearContent();
// Step 2 - Read and the data for ColA to ColK from the source sheet
//------------------------------------------------------------------
// Simulating this formula
//=query(IMPORTRANGE("1rm31Zza8fMS2pASIuFvQ0WBBqWb-174lD5VrtAixDjg","'Order Tracker'!A:T"),"select Col1, Col2, Col3, Col5, Col9, Col10, Col11, Col12, Col13, Col14,
// Col19 where Col10 = 'New' OR Col10 = 'Packed' OR Col10 = 'Pending' OR(Col10 = 'Delivered' AND Col14 > 0.01)",1)
var sourceSheet = SpreadsheetApp.openById('1LU-dSlGqyiKj6xjo5AVvNNdf1pBR26NTuaXZBdLK2Og').getSheetByName("Order Tracker");
var dataRange = sourceSheet.getDataRange();
var dataValues = dataRange.getValues().filter(function (x) {return x[9]=='New' || x[9] =='Packed' || x[9] == 'Pending' || (x[9] == 'Delivered' && x[13] >=0.01);});
// Remove columns we dont need.
var reqValues = [];
var reqCols=[0,1,2,4,8,9,10,11,12,13,18]; // corresponding to Col1, Col2 etc
for(var row=0; row<dataValues.length; row++){
var thisRow = [];
for (var col=0; col<reqCols.length; col++){
thisRow.push(dataValues[row][reqCols[col]]);
}
// Add placeholders cols for ColL and ColM
thisRow.push("None");
thisRow.push("None");
thisRow.push("None"); // to be removed later
reqValues.push(thisRow);
}
// Step 3 - Populate ColL and ColM - re-aligning the Invoice Numbers
//------------------------------------------------------------------
for (var row=0; row < reqValues.length; row++){
if (invNoList.hasOwnProperty(reqValues[row][0])){
var invNoIndex= invNoList[reqValues[row][0]]; // locate correct data based on invoice number
reqValues[row][11] = driverArray[invNoIndex][0]; // fill in Delivery Date
reqValues[row][12] = driverArray[invNoIndex][1]; // fill in the Driver
// below line to be removed later
reqValues[row][13] = driverArray[invNoIndex][2]; // fill in the CrossCheck data
}
}
//Step 4 - Copy the reqValues
//-----------------------------------------------
var finalRange = openSheet.getRange(2,1,reqValues.length,14); // openSheet and lastRow should be still valid
finalRange.setValues(reqValues);
//Done
}
我已经在您的测试表副本中对此进行了测试,一切似乎都正常。此链接是您的履行单的一个版本,并在 Col M 中安装了脚本和刷新按钮:https ://docs.google.com/spreadsheets/d/15ecr9CmXn2YyhMpGTg8VCVf8tTi5GaGrjgmQus9FxWA/edit?usp=sharing
任何 Google 脚本专家的注意事项:我必须制作原始“ query(IMPORTRANGE..)
”公式的脚本版本。这是第 2 步。如果有人看到更好的方法来做到这一点,我很想听听。我这样做的原因是由于谷歌脚本限制(据我了解):
query(IMPORTRANGE..)
执行“ ”后没有事件
- 如果我在脚本中安装查询,则无法在脚本中执行它。