简体   繁体   English

使用 For 循环运行 Apps 脚本 - Google 表格 / Apps 脚本

[英]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.

相关问题 在当前工作簿中没有的工作表上运行 Apps 脚本 - Google Sheets / Apps 脚本 - Run Apps Script on a Sheet not in Current Workbook - Google Sheets / Apps Script 谷歌表格应用程序脚本 - google sheets apps script Google Sheets Apps 脚本 - 带 if 语句的批处理循环 - Google Sheets Apps Script - batch loop with if statement 运行包含 If 语句的应用程序脚本代码(如何自动触发?) - Google Apps Script / Google Sheets - Run an apps script code containing an If Statement (How To Trigger Automatically ?) - Google Apps Script / Google Sheets 如果尝试运行应用程序脚本时范围包含零或空白单元格 - ALERT - Google Apps Script / Google Sheets - If Range contains Zero or Blank Cell when trying to run apps script - ALERT - Google Apps Script / Google Sheets 部署和使用带有Google Apps脚本的Google表格插件 - Deploy and use Google Sheets add-on with Google Apps Script 用于 Google 表格的 Google Apps 脚本的 JavaScript 中的 VBA 等效项 - VBA equivalent in JavaScript for use in Google Apps Script for Google Sheets 是否可以使用Google Apps脚本打印Google表格工作表? - Any way to use Google Apps Script to print a Google Sheets worksheet? 使用谷歌应用程序脚本将谷歌表格上的列增加 1? - Use google apps script to increment a column on Google Sheets by 1? Google Sheets + Apps 脚本:循环通过 ID 移动文件 - Google Sheets + Apps Script: Loop through ID to move files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM