简体   繁体   English

将一系列值复制/粘贴到单独工作表上的下一组空单元格中

[英]Copy/Pasting a Range of Values into the Next Empty Set of Cells on a Separate Sheet

I am attempting to take data from a "worksheet" which will always begin as the active sheet into a seperate sheet that is determined by the month of the year.我试图从始终作为活动工作表开始的“工作表”中获取数据,并将其放入由一年中的月份确定的单独工作表中。 That has been no problem so far and my code does function in this way.到目前为止这没有问题,我的代码确实以这种方式运行。 However I can not understand how to take that predetermined amount of info "A9:AG11" in our static worksheet and paste into the next empty set of rows on our shared monthly sheet.但是我不明白如何在我们的静态工作表中获取预定数量的信息“A9:AG11”并粘贴到我们共享的月度表上的下一组空行中。 Here is my current code that does work, ui button, and all.这是我当前有效的代码、ui 按钮和所有代码。 But it will of course only write over any data already existing on the second sheet.但它当然只会覆盖第二张纸上已经存在的任何数据。 Any help would be appreciated!任何帮助,将不胜感激!

function BP3CallTab() {
  var ui = SpreadsheetApp.getUi(); // Same variations.

  var result = ui.alert(
     'Please confirm',
     'Ready to Submit 3-Call Audit?',
      ui.ButtonSet.YES_NO);

  // Process the user's response.
  if (result == ui.Button.YES) {
    // User clicked "Yes".
    copyFunction ();
  }

    function copyFunction () {
      var inputRange =  SpreadsheetApp.getActiveSpreadsheet().getRange("A9:AG11");
      var inputValues = inputRange.getValues();
      var shname = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Dash").getRange("Q4").getValue()  
      var emptyrow = SpreadsheetApp.getActive();
       emptyrow.getRange('A17').activate();
       emptyrow.getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.DOWN).activate();
       emptyrow.getCurrentCell().offset(1, 0).activate();
      var outputRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(shname).getRange("A17:AG19").setValues(inputValues);
    }

    ui.alert('Audit Complete!✅');

  if (result ==ui.Button.NO) {
    // User clicked "No" or X in the title bar.
    ui.alert('No Changes Made.');
  }
};

You need to use a different method to define the output range.您需要使用不同的方法来定义输出范围。

  • Try getRange(row, column, numRows, numColumns) .试试getRange(row, column, numRows, numColumns)

  • "row" is obtained by using getLastRow() , say... “行”是通过使用getLastRow()获得的,比如说......

    • var targetLR = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(shname).getLastRow()
  • "Column" =1 (to denote column A) "Column" =1(表示 A 列)

  • "numRows" = 3 (the number of rows between "A17:A19") “numRows”= 3(“A17:A19”之间的行数)

  • "numColumns" = 33 (the number of columns between "A" and "AG") “numColumns”= 33(“A”和“AG”之间的列数)

So your output range would be something like:所以你的输出范围是这样的:

  • var outputRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(shname).getRange(targetLR,1,2,33).setValues(inputValues)

A slight variation of @Tedinoz's approach so that (a) the destination range is always exactly the same as the size of data being copied, otherwise it results to an error and (b) the checking of the next empty row is still correct even if a cell somewhere down below was accidentally edited --- something that affects getLastRow() @Tedinoz 的方法略有不同,因此 (a) 目标范围始终与被复制数据的大小完全相同,否则会导致错误,并且 (b) 下一个空行的检查仍然正确,即使下面某处的单元格被意外编辑了——影响 getLastRow() 的东西

function copyFunction () {
  var inputRange =  SpreadsheetApp.getActiveSpreadsheet().getRange("A9:AG11");
  var inputValues = inputRange.getValues();
  var inputValuesRows = inputValues.length;     // no. of rows of copied data
  var inputValuesCols = inputValues[0].length;  // no. of columns of copied data

  //get your destination sheet and its first empty row
  var sharedSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Shared Monthly Sheet");
  var firstEmptyRow = getFirstEmptyWholeRow("Shared Monthly Sheet");
  Logger.log(firstEmptyRow);

  //copy your data into your destination starting from firstEmptyRow and column A
  sharedSheet.getRange(firstEmptyRow,1,inputValuesRows,inputValuesCols).setValues(inputValues);
}

/*
 * Adapted from Mogsdad's "whole row" checker 
 * from https://stackoverflow.com/questions/6882104/faster-way-to-find-the-first-empty-row-in-a-google-sheet-column
 */
function getFirstEmptyWholeRow(sheetname) {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetname);
  var range = sheet.getDataRange();
  var values = range.getValues();
  var row = 0;
  for (var row=0; row<values.length; row++) {
    if (!values[row].join("")) break;
  }
  return (row+1);
}

暂无
暂无

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

相关问题 将单元格的值复制并粘贴到另一个电子表格,创建新工作表并粘贴值 - Copy and Paste Values of Cells to another Spreadsheet, creating a new sheet and pasting values 将单独单元格复制粘贴的脚本更改为多行的复制粘贴 - Change script for copy-pasting of separate cells to copy-pasting of several rows Select 单元格(Ctrl+A)然后复制范围并粘贴到另一个工作表的第一个空行 - Select cells(Ctrl+A) then Copy range and paste to another sheet's first empty row 在 Google App Script 中复制粘贴单元格范围的最快方法 - Quickest method of copy-pasting range of cells in Google App Script Google Spreadsheets 脚本 - 如何将范围从一张纸复制到另一张纸,但不覆盖非空目标单元格 - Google Spreadsheets script - How to copy range from one sheet to another sheet, but without overwriting non-empty target cells 值增加时将单元格复制到另一个工作表 - Copy cells to another sheet when values increase 基于不同工作表中的值的单元格颜色范围 - Color range of cells based on values in different sheet 复制一个范围并仅将带有值的行粘贴到下一个空白行的另一个工作表中? - Copy a range and paste only rows with values into another sheet at the next blank row? 如何从一个工作表中复制有效范围并将数据粘贴到Google工作表中同一工作簿中不同工作表中的特定范围内? - How to copy active range from one sheet and pasting the data to a specific range in a different sheet within the same workbook in google sheets? Google脚本-带有“如果为空”检查的多单元格从工作表复制到另一个 - Google Script - Multi cells copy from a sheet to another with “if empty” check
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM