简体   繁体   中英

Change script for copy-pasting of separate cells to copy-pasting of several rows

I use a function that allows me to copy-paste the data from the user form (sheet "Form") to the sheet "Data" by using the button "Submit" with the following script assigned to this button:

function submitData() {
  var ss        = SpreadsheetApp.getActiveSpreadsheet();
  var formSS    = ss.getSheetByName("Form"); //Form Sheet
  var datasheet = ss.getSheetByName("Data"); //Data Sheet
   
  //Input Values
  var values = [[formSS.getRange("A3").getValue(),
                 formSS.getRange("B3").getValue(),
                 formSS.getRange("C3").getValue(),
                 formSS.getRange("D3").getValue(),
                 formSS.getRange("E3").getValue(),
                 formSS.getRange("F3").getValue()]];
   
  datasheet.getRange(datasheet.getLastRow()+1, 1, 1, 6).setValues(values);

}

So, I fill the cells from A3 to F3, then press the button and the info goes to the sheet Data. Another entering goes to the Data sheet as the next row. Actually I copy-paste the row A3:F3 and it works good, but now I need to copy-paste several rows at one time: the range A3:F10. How should I change the script for this purpose?

Solution:

To copy and paste an entire range of values you can use getValues() and setValues() .

function submitData() {
  var ss        = SpreadsheetApp.getActiveSpreadsheet();
  var formSS    = ss.getSheetByName("Form"); //Form Sheet
  var datasheet = ss.getSheetByName("Data"); //Data Sheet
   
  //Input Values
  var values = formSS.getRange("A3:F10").getValues();
   
  datasheet.getRange(datasheet.getLastRow()+1, 1, 8, 6).setValues(values);

}

Sample Output:

Input:

在此处输入图像描述

Output:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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