整个脚本依赖于For循环和If语句的两种组合。两者基本相同。但是由于某种原因,我可以让第一部分工作,而第二部分无法工作。
我在下面的脚本中添加了注释。谢谢。
/** @OnlyCurrentDoc */
function onEdit(e) {
//This part ensures the main script only runs if the cell F6 in the sheet "Daily Data" is edited.
if (
e.source.getSheetName() == "Daily Data" &&
e.range.columnStart == 6 &&
e.range.columnEnd == 6 &&
e.range.rowStart >= 6 &&
e.range.rowEnd <= 6
) {
//Secction 1: This section finds the lowest # from a list of numbers by finding the lowest empty cell in coloumn D using an If statement:
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var daily_data = spreadsheet.getSheetByName("Daily Data");
var entirelistoftimes = daily_data.getRange(3, 4, 62).getValues();
for(var i=0; i<entirelistoftimes.length ; i++){ //This For loop will run through the entire D column in sheet "Daily Data".
if (entirelistoftimes[i] == ""){ //This If statement will look for the first empty cell.
//Copies the Total Number:
var TotalNo = daily_data.getRange(i+2, 2).getValues(); //Gets the # from the cell next to the last filled cell in column D.
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Daily Data'), true);
spreadsheet.getRange('F8').setValues(TotalNo); //Displays the # in a cell F8 in sheet "Daily Data".
//Stop once the first blank cell has been found:
break;
}
}
//THIS IS THE SECTION I CANNOT GET TO WORK:
//Section 2: This section uses the # we got from the above section to find a time from a the corresponding row of sheet "Long Term Data":
var LTD_data = spreadsheet.getSheetByName("Long Term Data");
var LTD_data_entirelistoftimes = LTD_data.getRange(6, 2, 65).getValues();
for(var j=0; j<LTD_data_entirelistoftimes.length ; j++){ //This For loop will run through the Long Term Data sheet, through the list of numbers column.
if (LTD_data_entirelistoftimes[j] == TotalNo){ //This if statement will look through column B from row 6 for the # we got from section 1 above.
//Copies the time from column D in Lon:
var YesterdayTime = LTD_data.getRange(j, 4).getValues(); //Gets the time from column D in row j in the "Long Term Data" Sheet.
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Daily Data'), true);
spreadsheet.getRange('F9').setValues(YesterdayTime); //Displays the time from the time underneath the # in sheet "Daily Data".
//Stop once the above has been completed:
break;
}
}
}
}
;
答案 0 :(得分:3)
如果您的脚本被修改,那么下面的修改如何?
if (LTD_data_entirelistoftimes[j] == TotalNo){
处的LTD_data_entirelistoftimes[j]
和TotalNo
分别是1维数组和2维数组。因为getValues()
返回二维数组。我认为您遇到问题的原因是直接比较这些值。请按如下所示修改脚本。
将==
用作比较运算符时,可以进行如下修改。
if (LTD_data_entirelistoftimes[j] == TotalNo){
至:
if (LTD_data_entirelistoftimes[j] == TotalNo[0][0]){
或
if (LTD_data_entirelistoftimes[j][0] == TotalNo[0][0]){
将===
用作比较运算符时,可以进行如下修改。
if (LTD_data_entirelistoftimes[j] == TotalNo){
至:
if (LTD_data_entirelistoftimes[j][0] === TotalNo[0][0]){
if (entirelistoftimes[i] == ""){
,==
用作比较运算符。因此,if语句有效。如果此修改无法解决您的问题,我深表歉意。那时,您可以提供示例电子表格吗?由此,我想对其进行修改。