Google表格脚本 - 重复多个值X次

时间:2018-02-04 04:14:05

标签: google-apps-script google-sheets google-spreadsheet-api google-sheets-api

我制作了一个工作脚本(下面),在一列(AB)中重复1-50次,每次8次。我只是为行编号。

我是新手(就像第三天一样)并且我确定有更有效的方法来做到这一点,我只是不知道那是什么。

初始设置(和更改变量)不会真正节省复制和粘贴大量时间的时间。虽然有内置函数(如REPT和ROW)可以接近,但它仍然需要复制/粘贴50次并更改单元格引用(由于锁定单元格和相对单元格引用的限制)。

如果有人能帮助(并希望解释)如何以更好的方式做到这一点,我们将不胜感激!

function numberGameAtA30() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet2 = SpreadsheetApp.setActiveSheet(ss.getSheetByName('GAMES2'));

var value1 = 1;

var value2 = 2;

var value3 = 3;

var value4 = 4;

var value5 = 5;

var value6 = 6;

var value7 = 7;

var value8 = 8;

var value9 = 9;

var value10 = 10;

var value11 = 11;

var value12 = 12;

var value13 = 13;

var value14 = 14;

var value15 = 15;

var value16 = 16;

var value17 = 17;

var value18 = 18;

var value19 = 19;

var value20 = 20;

var value21 = 21;

var value22 = 22;

var value23 = 23;

var value24 = 24;

var value25 = 25;

var value26 = 26;

var value27 = 27;

var value28 = 28;

var value29 = 29;

var value30 = 30;

var value31 = 31;

var value32 = 32;

var value33 = 33;

var value34 = 34;

var value35 = 35;

var value36 = 36;

var value37 = 37;

var value38 = 38;

var value39 = 39;

var value40 = 40;

var value41 = 41;

var value42 = 42;

var value43 = 43;

var value44 = 44;

var value45 = 45;

var value46 = 46;

var value47 = 47;

var value48 = 48;

var value49 = 49;

var value50 = 50;

// start columns and rows
  var startRow = 30
  var startColumn = 1;
  var numRows = 8;
  var numColumns = 1;

// ranges where values will be placed

var target1 = sheet2.getRange(startRow, startColumn, numRows, numColumns).setValue(value1);
var target2 = sheet2.getRange(startRow+8, startColumn, numRows, numColumns).setValue(value2);
var target3 = sheet2.getRange(startRow+16, startColumn, numRows, numColumns).setValue(value3);
var target4 = sheet2.getRange(startRow+24, startColumn, numRows, numColumns).setValue(value4);
var target5 = sheet2.getRange(startRow+32, startColumn, numRows, numColumns).setValue(value5);
var target6 = sheet2.getRange(startRow+40, startColumn, numRows, numColumns).setValue(value6);
var target7 = sheet2.getRange(startRow+48, startColumn, numRows, numColumns).setValue(value7);
var target8 = sheet2.getRange(startRow+56, startColumn, numRows, numColumns).setValue(value8);
var target9 = sheet2.getRange(startRow+64, startColumn, numRows, numColumns).setValue(value9);
var target10 = sheet2.getRange(startRow+72, startColumn, numRows, numColumns).setValue(value10);
var target11 = sheet2.getRange(startRow+80, startColumn, numRows, numColumns).setValue(value11);
var target12 = sheet2.getRange(startRow+88, startColumn, numRows, numColumns).setValue(value12);
var target13 = sheet2.getRange(startRow+96, startColumn, numRows, numColumns).setValue(value13);
var target14 = sheet2.getRange(startRow+104, startColumn, numRows, numColumns).setValue(value14);
var target15 = sheet2.getRange(startRow+112, startColumn, numRows, numColumns).setValue(value15);
var target16 = sheet2.getRange(startRow+120, startColumn, numRows, numColumns).setValue(value16);
var target17 = sheet2.getRange(startRow+128, startColumn, numRows, numColumns).setValue(value17);
var target18 = sheet2.getRange(startRow+136, startColumn, numRows, numColumns).setValue(value18);
var target19 = sheet2.getRange(startRow+144, startColumn, numRows, numColumns).setValue(value19);
var target20 = sheet2.getRange(startRow+152, startColumn, numRows, numColumns).setValue(value20);
var target21 = sheet2.getRange(startRow+160, startColumn, numRows, numColumns).setValue(value21);
var target22 = sheet2.getRange(startRow+168, startColumn, numRows, numColumns).setValue(value22);
var target23 = sheet2.getRange(startRow+176, startColumn, numRows, numColumns).setValue(value23);
var target24 = sheet2.getRange(startRow+184, startColumn, numRows, numColumns).setValue(value24);
var target25 = sheet2.getRange(startRow+192, startColumn, numRows, numColumns).setValue(value25);
var target26 = sheet2.getRange(startRow+200, startColumn, numRows, numColumns).setValue(value26);
var target27 = sheet2.getRange(startRow+208, startColumn, numRows, numColumns).setValue(value27);
var target28 = sheet2.getRange(startRow+216, startColumn, numRows, numColumns).setValue(value28);
var target29 = sheet2.getRange(startRow+224, startColumn, numRows, numColumns).setValue(value29);
var target30 = sheet2.getRange(startRow+232, startColumn, numRows, numColumns).setValue(value30);
var target31 = sheet2.getRange(startRow+240, startColumn, numRows, numColumns).setValue(value31);
var target32 = sheet2.getRange(startRow+248, startColumn, numRows, numColumns).setValue(value32);
var target33 = sheet2.getRange(startRow+256, startColumn, numRows, numColumns).setValue(value33);
var target34 = sheet2.getRange(startRow+264, startColumn, numRows, numColumns).setValue(value34);
var target35 = sheet2.getRange(startRow+272, startColumn, numRows, numColumns).setValue(value35);
var target36 = sheet2.getRange(startRow+280, startColumn, numRows, numColumns).setValue(value36);
var target37 = sheet2.getRange(startRow+288, startColumn, numRows, numColumns).setValue(value37);
var target38 = sheet2.getRange(startRow+296, startColumn, numRows, numColumns).setValue(value38);
var target39 = sheet2.getRange(startRow+304, startColumn, numRows, numColumns).setValue(value39);
var target40 = sheet2.getRange(startRow+312, startColumn, numRows, numColumns).setValue(value40);
var target41 = sheet2.getRange(startRow+320, startColumn, numRows, numColumns).setValue(value41);
var target42 = sheet2.getRange(startRow+328, startColumn, numRows, numColumns).setValue(value42);
var target43 = sheet2.getRange(startRow+336, startColumn, numRows, numColumns).setValue(value43);
var target44 = sheet2.getRange(startRow+344, startColumn, numRows, numColumns).setValue(value44);
var target45 = sheet2.getRange(startRow+352, startColumn, numRows, numColumns).setValue(value45);
var target46 = sheet2.getRange(startRow+360, startColumn, numRows, numColumns).setValue(value46);
var target47 = sheet2.getRange(startRow+368, startColumn, numRows, numColumns).setValue(value47);
var target48 = sheet2.getRange(startRow+376, startColumn, numRows, numColumns).setValue(value48);
var target49 = sheet2.getRange(startRow+384, startColumn, numRows, numColumns).setValue(value49);
var target50 = sheet2.getRange(startRow+392, startColumn, numRows, numColumns).setValue(value50);

}

2 个答案:

答案 0 :(得分:1)

试试这个:它使用for循环在括号内转50次。

function numberGameAtA30() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet2 = SpreadsheetApp.setActiveSheet(ss.getSheetByName('GAMES2'));

// start columns and rows
  var currentRow = 30
  var startColumn = 1;
  var numRows = 8;
  var numColumns = 1;


// ranges where values will be placed
for(i = 1;i <= 50;i++){
var target1 = sheet2.getRange(currentRow, startColumn, numRows, numColumns).setValue(i);
currentRow = currentRow + 8;
}

答案 1 :(得分:1)

这应该快得多......请参阅best practices

    Sample motif chromosome
    1      CT-G.A    1
    1      TA-C.C    1
    1      TC-G.C    2
    2      CG-A.T    2
    2      CA-G.T    2