gpt4 book ai didi

Apps Script w/ Google Sheets comparing values on different columns and rows in a loop(使用Google工作表在循环中比较不同列和行上的值的应用程序脚本)

转载 作者:bug小助手 更新时间:2023-10-26 20:41:03 25 4
gpt4 key购买 nike



The Code:

《守则》:


function ArrayRowCheck() {

// Find the last row in the array
const ss = SpreadsheetApp.getActiveSpreadsheet()
const sheet = ss.getSheetByName("15min");
const lastRow = sheet.getRange("C2").getNextDataCell(SpreadsheetApp.Direction.DOWN).getRow().toFixed();
const z = sheet.getRange("C2:G" + sheet.getLastRow())
const a = z.getValues()

for (let i = lastRow - 2; i >= 0; i--) {

if (a[i][1] > a[i][4]) {
console.log("True")

}else {
console.log("False")
}

}
}

The Problem:

问题是:


In the for loop I want to be able to repetitively compare column 1 and 4 between two rows but I'm unsure how to introduce the next row up into the loop.

在for循环中,我希望能够在两行之间重复比较第1列和第4列,但我不确定如何在循环中引入下一行。


Example:

示例:


Compare row 10 column 1 with row 9 column 4
next, compare row 9 column 4 with row 8 column 1
then, compare row 8 column 1 with row 7 column 4

接下来比较第10行第1列和第9列第4列,然后比较第9行第4列和第8列第1列,然后比较第8行第1列和第7列第4列


until we run out of rows to compare. I need access to column 1 and 4 from every row.

直到我们用完了可供比较的行。我需要从每一行进入第一列和第四列。


更多回答

I would use an object that has the two rows in an array and is indexed zero through whatever and then on the index of the loop use the modulo operator to index into your object hopefully that makes sense to you

我将使用一个对象,该对象在一个数组中有两行,并且索引为零,然后在循环的索引上,使用模运算符来索引您的对象,希望这对您有意义

Although I'm not sure whether I could correctly understand your expected result, I proposed an answer. Please confirm it. If I misunderstood your question and that was not useful, I apologize.

虽然我不确定我是否能正确理解您的预期结果,但我提出了一个答案。请确认一下。如果我误解了你的问题,这是没有用的,我道歉。

优秀答案推荐

Modification points:



  • About Compare row 10 column 1 with row 9 column 4 next, compare row 9 column 4 with row 8 column 1 then, compare row 8 column 1 with row 7 column 4, if you want to check columns "A" and "D", sheet.getRange("C2:G" + sheet.getLastRow()) might be required to be sheet.getRange("A2:D" + sheet.getLastRow()).

    关于比较第10行第1列与第9行第4列,然后将第9行第4列与第8行第1列进行比较,再将第8行第1列与第7列第4列进行比较,如果要检查列“A”和“D”,则sheet.getRange(“C2:G”+sheet.getLastRow())可能需要为sheet.getRange(“A2:D”+sheet.getLastRow())。



  • I thought that for (let i = lastRow - 2; i >= 0; i--) { might be for (let i = a.length - 1; i > 0; i--) {.

    我认为for(设i=lastRow-2;i>=0;i--){可能是for(设i=a.long-1;i>0;i--){。



  • From a[i][1] > a[i][4], the 1st index of the array is 0. Please be careful about this.

    从a[i][1]>a[i][4]开始,数组的第一个索引为0。请注意这一点。




When these points are refected in your script, how about the following modification?

当这些点被反映在您的脚本中时,下面的修改如何?


Modified script:


function ArrayRowCheck() {
const ss = SpreadsheetApp.getActiveSpreadsheet()
const sheet = ss.getSheetByName("15min");
const z = sheet.getRange("A2:D" + sheet.getLastRow());
const a = z.getValues();
for (let i = a.length - 1; i > 0; i--) {
if (a[i][0] > a[i - 1][3]) {
console.log("True");
} else {
console.log("False");
}
}
}


  • In this case, the value of cell "A2" cannot be compared because of no value of "D1". So, let i = a.length - 1; i > 0; i-- is used as the condition of loop.


References:




You want to compare values in an array but are unsure about unsure how to introduce the next row up into the loop.

您希望比较数组中的值,但不确定如何将下一行引入循环。


In the example, comparisons cover four rows:

在该示例中,比较涉及四行:



  • row 10 column 1 with row 9 column 4

    第10行第1列,第9行第4列



  • row 9 column 4 with row 8 column 1

    第9行第4列,第8行第1列



  • row 8 column 1 with row 7 column 4

    第8行第1列第7行第4列



  • You do NOT have to introduce the other rows since they are accessible within the array value using the condition value. The initialisation statement sets i = 3 this is because a value of "0", "1" or "2" would not enable the script to access a value four rows above any given row.

    您不必引入其他行,因为可以使用Condition值在数组值中访问它们。初始化语句设置i=3这是因为值“0”、“1”或“2”不会使脚本能够访问任何给定行上四行的值。



  • The values in rows "above" the current row can be accessed by adjusting the array value. For example:

    可以通过调整数组值来访问当前行“上方”的行中的值。例如:



    • var value1 = data[i][0] // row3 column1

    • var value2 = data[i-1][3] // row2, column4






function arrayRowCheck() {

// Find the last row in the array
var ss = SpreadsheetApp.getActiveSpreadsheet()
var sheet = ss.getSheetByName("15min");

var aVals = sheet.getRange("A2:A").getValues();
var aLast = aVals.filter(String).length;
//Logger.log("DEBUG: Number of rows of data = "+aLast)

// get data=
var data = sheet.getRange(2,1,aLast,4).getValues()
//Logger.log("DEBUG: the data range = "+sheet.getRange(2,1,aLast,4).getA1Notation())

// loop through the data
// must start on data row#3 (zero-based index)
// since logic compares results for previopus two rows

for (var i=3;i<data.length;i++){
var value1 = data[i][0] // row3 column1
var value2 = data[i-1][3] // row2, column4
var value3 = data[i-2][0] // row1 column1
var value4 = data[i-3][3] // row0 column4
//Logger.log("DEBUG: i="+i+", value1 = "+value1+", value2 = "+value2+", value3 = "+value3+", value4 = "+value4)
// Comparison #1
if (value1 > value2) {
//console.log("DEBUG: Comparison#1 (value1 vs value2): True")

}else {
//console.log("DEBUG: Comparison#1 (value1 vs value2): False")
}
// Comparison #2
if (value2 > value3) {
//console.log("DEBUG: Comparison#2 (value2 vs value3): True")

}else {
//console.log("DEBUG: Comparison#2 (value2 vs value3): False")
}
// Comparison #2
if (value3 > value4) {
//console.log("DEBUG: Comparison#3 (value3 vs value4): True")

}else {
//console.log("DEBUG: Comparison#3 (value3 vs value4): False")
}
}
}



SAMPLE DATA

样本数据


sample


SAMPLE RESULTS

样本结果


results


更多回答

this is what i was looking for. I was missing that you can subtract from i within the array element. Also, thanks for the clean up of unnecessary variables.

这就是我要找的东西。你可以在数组元素中从i中减去。另外,感谢您清理了不必要的变量。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com