工作表脚本:如果A列中的值为true,则将C列设置为更高

时间:2018-07-24 22:21:28

标签: google-apps-script google-sheets

我正在使用一个脚本,该脚本使用A列中的一列复选框,B列中的名称列表,然后对B列中的每个名称检查A列中的复选框的运行总数我让脚本在整个范围A内运行,寻找“ True”语句(选中的框),然后将结果应用于C列中的单元格...但是它不能正常工作。除了增加C单元格中的值外,我只能将其设置为1或将值设置为列1 + 1的整个结果字符串。如何使C列中的每个单元格增加1仅当相应名称旁边的复选框被选中时才1?

这是到目前为止的脚本:

function setValues() {
//Get the sheet you want to work with. 
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet1");
//Grab the entire Range, and grab whatever values you need from it. EX: rangevalues
var range = sheet.getRange("A1:A10");
var range2 = sheet.getRange("C1:C10");
var range2values = range2.getValues();
var rangevalues = range.getValues();
//Loops through range results
for (var i in rangevalues) {
 for (var j in rangevalues) {
 //Get the x,y location of the current cell.
     var x = parseInt(j, 10) + 1;
     var y = parseInt(i, 10) + 1;
 //Set the rules logic
    if (rangevalues[i][j] == 1) {
    //Set the cell 
       sheet.getRange(y,x+2).setValue(range2values+1);
   }
  }
 }
}

该脚本还有更多内容可以告诉它何时运行,以及在每次脚本运行后清除复选框,但是一旦确定了这一部分,我将在最后添加这些复选框。

>

2 个答案:

答案 0 :(得分:1)

数组结构:

A1:A4 rangeValues看起来像这样:

[
[A1],
[A2],
[A3],
[A4]
   ]

rangeValues [0]是数组:

[A1]

rangeValues [1]是数组:

[A2]

rangeValues [0] [0]是元素/值:

A1

rangeValues [0] [1]是值:

undefined

A1:B4 rangeValues看起来像这样:

[
[A1,B1],
[A2,B2],
[A3,B3],
[A4,B4]
     ]

现在,
rangeValues [0]是数组:

[A1,B1]

rangeValues [0] [0]是元素/值:

A1

rangeValues [0] [1]是元素/值:

B1

数组按行索引,然后按零开始的列索引。


对于A1:A10 rangeValues,您需要在数组中的每个值上循环10次。 使用时,

for (var i in rangevalues) {
 for (var j in rangevalues) {

您实际上不必要地循环10 * 10次:如果在此循环中使用Logger.log("rangevalues["+i+"]["+j+"] is:"+rangevalues[i][j]),则会得到以下日志:

[--Time--:473] rangevalues[0][0] is:false
[--Time--:473] rangevalues[0][1] is:undefined
[--Time--:474] rangevalues[0][2] is:undefined
[--Time--:475] rangevalues[0][3] is:undefined
[--Time--:476] rangevalues[0][4] is:undefined
[--Time--:476] rangevalues[0][5] is:undefined
[--Time--:477] rangevalues[0][6] is:undefined
[--Time--:478] rangevalues[0][7] is:undefined
[--Time--:479] rangevalues[0][8] is:undefined
[--Time--:479] rangevalues[0][9] is:undefined
[--Time--:480] rangevalues[1][0] is:
[--Time--:481] rangevalues[1][1] is:undefined
[--Time--:481] rangevalues[1][2] is:undefined
[--Time--:482] rangevalues[1][3] is:undefined
[--Time--:483] rangevalues[1][4] is:undefined
[--Time--:484] rangevalues[1][5] is:undefined
[--Time--:484] rangevalues[1][6] is:undefined
[--Time--:485] rangevalues[1][7] is:undefined
[--Time--:486] rangevalues[1][8] is:undefined
[--Time--:486] rangevalues[1][9] is:undefined
[--Time--:487] rangevalues[2][0] is:false
[--Time--:488] rangevalues[2][1] is:undefined
[--Time--:488] rangevalues[2][2] is:undefined
[--Time--:489] rangevalues[2][3] is:undefined
[--Time--:490] rangevalues[2][4] is:undefined
[--Time--:490] rangevalues[2][5] is:undefined
[--Time--:491] rangevalues[2][6] is:undefined
[--Time--:492] rangevalues[2][7] is:undefined
[--Time--:492] rangevalues[2][8] is:undefined
[--Time--:493] rangevalues[2][9] is:undefined
[--Time--:494] rangevalues[3][0] is:
[--Time--:495] rangevalues[3][1] is:undefined
[--Time--:495] rangevalues[3][2] is:undefined
[--Time--:496] rangevalues[3][3] is:undefined
[--Time--:497] rangevalues[3][4] is:undefined
[--Time--:498] rangevalues[3][5] is:undefined
[--Time--:498] rangevalues[3][6] is:undefined
[--Time--:499] rangevalues[3][7] is:undefined
[--Time--:500] rangevalues[3][8] is:undefined
[--Time--:501] rangevalues[3][9] is:undefined
[--Time--:501] rangevalues[4][0] is:
[--Time--:502] rangevalues[4][1] is:undefined
[--Time--:503] rangevalues[4][2] is:undefined
[--Time--:503] rangevalues[4][3] is:undefined
[--Time--:504] rangevalues[4][4] is:undefined
[--Time--:505] rangevalues[4][5] is:undefined
[--Time--:506] rangevalues[4][6] is:undefined
[--Time--:506] rangevalues[4][7] is:undefined
[--Time--:507] rangevalues[4][8] is:undefined
[--Time--:507] rangevalues[4][9] is:undefined
[--Time--:508] rangevalues[5][0] is:
[--Time--:509] rangevalues[5][1] is:undefined
[--Time--:509] rangevalues[5][2] is:undefined
[--Time--:510] rangevalues[5][3] is:undefined
[--Time--:511] rangevalues[5][4] is:undefined
[--Time--:512] rangevalues[5][5] is:undefined
[--Time--:512] rangevalues[5][6] is:undefined
[--Time--:513] rangevalues[5][7] is:undefined
[--Time--:514] rangevalues[5][8] is:undefined
[--Time--:515] rangevalues[5][9] is:undefined
[--Time--:515] rangevalues[6][0] is:
[--Time--:516] rangevalues[6][1] is:undefined
[--Time--:517] rangevalues[6][2] is:undefined
[--Time--:517] rangevalues[6][3] is:undefined
[--Time--:518] rangevalues[6][4] is:undefined
[--Time--:519] rangevalues[6][5] is:undefined
[--Time--:519] rangevalues[6][6] is:undefined
[--Time--:520] rangevalues[6][7] is:undefined
[--Time--:521] rangevalues[6][8] is:undefined
[--Time--:522] rangevalues[6][9] is:undefined
[--Time--:522] rangevalues[7][0] is:
[--Time--:523] rangevalues[7][1] is:undefined
[--Time--:524] rangevalues[7][2] is:undefined
[--Time--:524] rangevalues[7][3] is:undefined
[--Time--:525] rangevalues[7][4] is:undefined
[--Time--:525] rangevalues[7][5] is:undefined
[--Time--:526] rangevalues[7][6] is:undefined
[--Time--:527] rangevalues[7][7] is:undefined
[--Time--:527] rangevalues[7][8] is:undefined
[--Time--:528] rangevalues[7][9] is:undefined
[--Time--:529] rangevalues[8][0] is:
[--Time--:529] rangevalues[8][1] is:undefined
[--Time--:530] rangevalues[8][2] is:undefined
[--Time--:531] rangevalues[8][3] is:undefined
[--Time--:531] rangevalues[8][4] is:undefined
[--Time--:532] rangevalues[8][5] is:undefined
[--Time--:533] rangevalues[8][6] is:undefined
[--Time--:533] rangevalues[8][7] is:undefined
[--Time--:534] rangevalues[8][8] is:undefined
[--Time--:535] rangevalues[8][9] is:undefined
[--Time--:535] rangevalues[9][0] is:true //A10 box is checked
[--Time--:536] rangevalues[9][1] is:undefined
[--Time--:536] rangevalues[9][2] is:undefined
[--Time--:537] rangevalues[9][3] is:undefined
[--Time--:538] rangevalues[9][4] is:undefined
[--Time--:538] rangevalues[9][5] is:undefined
[--Time--:539] rangevalues[9][6] is:undefined
[--Time--:539] rangevalues[9][7] is:undefined
[--Time--:540] rangevalues[9][8] is:undefined
[--Time--:541] rangevalues[9][9] is:undefined

由于只获得一列,因此只需要循环一次即可。换句话说,循环中唯一有效的j[0]。其他所有内容均未定义。

下一步,

sheet.getRange(y,x+2).setValue(range2values+1);

在这里,range2values是数组。您需要将值添加到相应的值。因此,这可能有效:

sheet.getRange(y,x+2).setValue(range2values[i][j]+1);

但是,使用setValue()并不是一个好习惯,因为您将价值100次写入电子表格中,这非常昂贵。始终使用批处理操作-创建输出数组,并改用setValues()


修改后的脚本:

//@OnlyCurrentDoc
function setValues() {
  //Get the sheet you want to work with. 
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Sheet1");
  //Grab the entire Range, and grab whatever values you need from it. EX: rangevalues
  var range = sheet.getRange("A1:A10");
  var range2 = sheet.getRange("C1:C10");
  var range2values = range2.getValues();
  var rangevalues = range.getValues();
  //Loops through range results
  for (var i in rangevalues) {
    // for (var j in rangevalues) {

    Logger.log("rangevalues["+i+"]["+0+"] is:"+rangevalues[i][0]);//Added

    //Get the x,y location of the current cell.
    //    var x = parseInt(j, 10) + 1;
    //    var y = parseInt(i, 10) + 1;
    //Set the rules logic
    if (rangevalues[i][0] == true) { //Modified
      //Set the cell 
      range2values[i][0] += 1; //Directly add 1 to range2values
      Logger.log(range2values);//Added
    }
  }
  range2.setValues(range2values);//Added; Set the modified range2values back to range2(Column C)
}

进一步阅读:

答案 1 :(得分:0)

只需将相应的行切换到sheet.getRange(y,x+2).setValue(1+parseInt(range2values[y-1],10)); 这样会将所需单元格中的值设置为比其原始值大一。注意range2values从零开始,因此我们需要-1。另外range2值是字符串,但我们想在相应的数字上加1。

您拥有的代码将单个单元格设置为(range2values + 1),但range2Values是一个数组。