我有一个脚本,其工作原理类似于在
Index Match
和 entry
之间复制过去的 database
。如果条目表 C7:I7
有文本值,但在其日期 14.11.2024
尝试过 Utilities.formatDate
但没有帮助时不起作用,那么它的工作就完美了。
脚本的作用
Check if an value in row 4 already appears in "database"
if yes, then update the value in Column B
if no, then append the row 4 and row 7 values to "database"
有人可以告诉我哪里错了吗?
function CopyEntry2Db() {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var entrySheet = ss.getSheetByName("entry")
var dbSheet = ss.getSheetByName("database")
// get Entry sheet row 4 and 7 and flatten to 1D arrays
var entryRow4 = entrySheet.getRange("C7:I7").getDisplayValues().flat()
//var entryRow4 = Utilities.formatDate(entryRow, "GMT+1", "dd/MM/yyyy");
var entryRow7 = entrySheet.getRange("C4:I4").getValues().flat()
// Logger.log(entryRow4) // DEBUG
// Logger.log(entryRow7) // DEBUG
// get database sheet columns A and B
var dbSheetLR = dbSheet.getLastRow()
// Logger.log("DEBUG: Last row of data on database = "+dbSheetLR+", so number of rows of data exlcuding header = "+(+dbSheetLR-1))
var dbColAValues = dbSheet.getRange(2,1,dbSheetLR-1,1).getValues().flat()
// Logger.log(dbColAValues) // DEBUG
var dbColBValues = dbSheet.getRange(2,2,dbSheetLR-1,1).getValues().flat()
// Logger.log(dbColBValues) // DEBUG
// create a temporary array to hold values to add to database
var tempArray = []
// loop though the row4 entry values to find a match on db column A
// if indexOf return value = -1, then no match, so add entry value to db
// if indexOf returns value <. -1, then value is array index
for (var i=0;i<entryRow4.length;i++){
var idxMatch = dbColAValues.indexOf(entryRow4[i]);
//Logger.log("DEBUG: The entry value = "+entryRow4[i]+", index match = "+idxMatch)
if (idxMatch == -1 ){
// no match, so add entry values to tempArray
tempArray.push([entryRow4[i],entryRow7[i]])
}else{
// Logger.log("matches value = the index = "+idxMatch+""+", Column A = "+entryRow4[i]+", new value = "+entryRow7[i])
// match, so update dbColB value
dbColBValues[i] = entryRow7[i]
}
}
// Logger.log(tempArray) // DEBUG
// Logger.log(dbColBValues) // DEBUG
// convert dbColBValues to 2d array
var nArr = [];
while(dbColBValues.length > 0) {
nArr.push(dbColBValues.splice(0,1));
}
// Logger.log("DEBUG: the Column B range = "+dbSheet.getRange(2,2,nArr.length,1).getA1Notation())
// update dbColBValues
dbSheet.getRange(2,2,nArr.length,1).setValues(nArr)
// copy new entry values to database Sheet
// Logger.log("the target range = "+dbSheet.getRange(dbSheetLR+1,1,tempArray.length,2).getA1Notation())
dbSheet.getRange(dbSheetLR+1,1,tempArray.length,2).setValues(tempArray)
}
变量设置不正确,导致了重大问题
从此:
var entryRow4 = entrySheet.getRange("C7:I7").getDisplayValues().flat()
var entryRow7 = entrySheet.getRange("C4:I4").getValues().flat()
对此:
var entryRow7 = entrySheet.getRange("C7:I7").getDisplayValues().flat();
var entryRow4 = entrySheet.getRange("C4:I4").getValues().flat();
并将 else 语句更改为:
if (idxMatch == -1 ){
tempArray.push([entryRow4[i],entryRow7[i]])
}else{
dbColBValues[i] = entryRow7[i]
}
致:
if (idxMatch == -1) {
tempArray.push([entryRow4[i], entryRow7[i]]);
} else {
dbColBValues[idxMatch] = entryRow7[i];
}
完整代码:
function CopyEntry2Db() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var entrySheet = ss.getSheetByName("entry");
var dbSheet = ss.getSheetByName("database");
var entryRow7 = entrySheet.getRange("C7:I7").getDisplayValues().flat();
var entryRow4 = entrySheet.getRange("C4:I4").getValues().flat();
var dbSheetLR = dbSheet.getLastRow();
var dbColAValues = dbSheet.getRange(2, 1, dbSheetLR - 1, 1).getValues().flat();
var dbColBValues = dbSheet.getRange(2, 2, dbSheetLR - 1, 1).getValues().flat();
var tempArray = [];
for (var i = 0; i < entryRow4.length; i++) {
var idxMatch = dbColAValues.indexOf(entryRow4[i]);
if (idxMatch == -1) {
tempArray.push([entryRow4[i], entryRow7[i]]);
} else {
dbColBValues[idxMatch] = entryRow7[i];
}
}
var nArr = [];
while (dbColBValues.length > 0) {
nArr.push(dbColBValues.splice(0, 1));
}
dbSheet.getRange(2, 2, nArr.length, 1).setValues(nArr);
if (tempArray.length > 0) {
dbSheet.getRange(dbSheetLR + 1, 1, tempArray.length, 2).setValues(tempArray);
}
}