简体   繁体   English

循环浏览Google脚本上的列

[英]Loop through columns on google scripts

I am looking to, with google scripts, loop through a number of input values and then copy paste value the resultant output values into another sheet with each new input iteration having the results pasted into successive columns. 我希望通过google脚本循环遍历多个输入值,然后将结果输出值粘贴粘贴到另一个工作表中,每次新输入迭代将结果粘贴到连续的列中。

I have managed to get the input vales to loop in an increment and to get the copy paste values to work in one column. 我设法使输入值递增循环,并使复制粘贴值能在一列中工作。 However, I can't work out how to get the column that results are pasted into to loop (ie paste into column A, then B etc.). 但是,我无法解决如何将结果列粘贴到循环中(即,粘贴到列A,然后粘贴到B等)。

I had a look on Stack Overflow, but could not find anything that appeared to answer this. 我看了一下Stack Overflow,但是找不到任何能回答这个问题的东西。 My current code can be found below: 我当前的代码可以在下面找到:

function ValueByOne() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var cell = ss.getRange('Inputs!F3');
  var cellValue = cell.getValue();
  for (var cellValue = 1000; cellValue < 80000; cellValue += 1000){
  cell.setValue(cellValue + 1000);
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var source = ss.getRange('CostComparison!D14:D27');
  source.copyTo(ss.getRange('Sheet9!A1'), {contentsOnly: true});
  }
  }

It is specifically the 'Sheet9!A1' that I want to loop. 我想循环播放的是'Sheet9!A1' Such that when cellValue is 1000 it pastes to 'Sheet9!A1' and when cellValue moves to 2000 it pastes to 'Sheet9!B1' . 这样,当cellValue为1000时,它将粘贴到'Sheet9!A1' ;当cellValue移至2000时,它将粘贴到'Sheet9!B1'

Thanks in advance for all of your help! 预先感谢您的所有帮助!

Jack 插口

Perhaps, you can try a different approach when copying the files. 也许,您可以在复制文件时尝试其他方法。 Instead of using the copyTo function, try using copyValuesToRange, so you can iterate on the columns. 代替使用copyTo函数,请尝试使用copyValuesToRange,以便可以在列上进行迭代。 The code should be something similar to this: 该代码应类似于以下内容:

var ss = SpreadsheetApp.getActiveSpreadsheet();
var cell = ss.getRange('Inputs!F3');
  var cellValue = cell.getValue();
  var resultsRange = ss.getRange('Results!A1:H1');
  var resultsSheet = ss.getSheetByName('Results');
  for (var cellValue = 1000; cellValue < 80000; cellValue += 1000){
    cell.setValue(cellValue + 1000);
    var source = ss.getRange('CostComparison!D14:D27');
    var col = cellValue / 1000;
    source.copyValuesToRange( resultsSheet, col, col, 1, 10 );
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM