[英]Use A For Loop to Run Apps Script - Google Sheets / Apps Script
I would like to repeat a piece of code with a For Loop every time it sees a blank cell in Column N.每次在 N 列中看到一个空白单元格时,我想用 For 循环重复一段代码。
I am trying the below code but cant figure out where I am going wrong我正在尝试下面的代码,但无法弄清楚我哪里出错了
function CopyCellBelow() {
// Get array of values in the search Range
var sheet = SpreadsheetApp.getActive();
var lastRow = sheet.getLastRow();
var searchRange = sheet.getRange(14,2, lastRow-1);
var rangeValues = searchRange.getValues();
for ( j = 0 ; j < lastRow - 1; j++){
if(rangeValues[j] === ""){
sheet.getRange(j+14);// Column N
sheet.getRange('N2').activate();
sheet.getRange('N3').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
sheet.getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.DOWN).activate();
sheet.getCurrentCell().offset(1, 0).activate();
sheet.getCurrentCell().offset(1, 0).copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
sheet.getRange('N2').activate();
sheet.getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.DOWN).activate();
sheet.getCurrentCell().offset(1, 0).activate();
sheet.getCurrentCell().offset(1, 0).copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
sheet.getRange('N1').activate();
}else if (rangeValues[j] === "if cell has data"){
//do nothing
}}};
In the image below you can see the yellow cells have the expected result which is copying the cell directly below在下图中,您可以看到黄色单元格具有预期的结果,即直接复制下面的单元格
The next 3 blanks cells should have a 4 placed in them接下来的 3 个空白单元格中应该有 4 个
See if this helps看看这是否有帮助
const copyCellBelow = () => {
const sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1'); //change sheet name if needed
const range = sheet.getRange(1, 14, sheet.getLastRow(), 1); //change range if needed
const data = range.getValues().map( (cell, i, val) => (cell[0]) ? cell : val[i
+ 1])
range.setValues(data);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.