我正在尝试运行一个条件来检查2个数组中的2个项目。我从Spreasheet中提取数据并在条件合适时复制。
例如我想比较电子表格中的2个单元格并复制高单元格中的数据并更新第二个单元格中的低数据,我是编码的新手,我可以得到建议。
function copyRANGEHIGH() {
var NEWHIGH = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("INDEX-RANGE");
var OLDHIGH = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("INDEX-RANGE");
var VALNEWHIGH1 = NEWHIGH.getRange("U4").getValues(); //DIA
var VALOLDHIGH1 = OLDHIGH.getRange("P5").getValues();
var VALNEWHIGH2 = NEWHIGH.getRange("U22").getValues();//SPY
var VALOLDHIGH2 = OLDHIGH.getRange("P23").getValues();
var VALNEWHIGH3 = NEWHIGH.getRange("U39").getValues();//IWM
var VALOLDHIGH3 = OLDHIGH.getRange("P40").getValues();
var arr1=[VALNEWHIGH1,VALNEWHIGH2,VALNEWHIGH3];
var arr2=[VALOLDHIGH1,VALOLDHIGH2,VALOLDHIGH3];
for (var i=0; i < arr1.length; i++) {
for (var j=0; j < arr2.length; j++) {
// Browser.msgBox(arr1[i])
if ( arr1[i] > arr2[j] ) {
答案 0 :(得分:0)
你的代码是不完整的,但是拿这两个数组,循环遍历它们并检查一个是否大于另一个可以这样做。
var arr1=[VALNEWHIGH1, VALNEWHIGH2, VALNEWHIGH3];
var arr2=[VALOLDHIGH1, VALOLDHIGH2, VALOLDHIGH3];
//You only need one loop, the two arrays are the same length,
//as long as the items you want to complare are at the same position
//in each array this will work.
for( var i = 0; i < arr1.length; i++ ) {
if( parseFloat(arr1[i]) > parseFloat(arr2[i]) ) { //parseFloat() converts the values to floating point numbers (decimals)
//Value in array 1 is higher
//Do something else here.
}
}
可以添加一些额外的代码,以便将值转换为数字更安全,以防您获得无法转换为数字的意外值,但上述代码应足以让您了解从哪里开始这里。