简体   繁体   中英

To copy Cell values from a range and paste it in a single row of another sheet

Wanted to accomplish below mentioned task, Having 2 Sheets(Sheet1 and Sheet 2) On clicking the Checkbox on Sheet 1:J1, data present in the table form on the Sheet 1 should get pasted in the single row of Sheet2 For eg Product 1 data (P1) to the Product 1 Column, Product 2 data (P2) to the Product 2 Column and so on... For every new submit, new data to be recorded on the next row of Sheet2. SS link: https://docs.google.com/spreadsheets/d/1tmnDOMyupjeO8d65qHQsxb5KrrKeq7pMYIE8h2VmEJ4/edit?usp=sharing

Thanks in advance

I believe your goal is as follows.

  • When a checkbox of "H1" of "Sheet1" is checked, you want to copy the values from the cells "A3:G6" to the columns "B" to "AC" in one row of "Sheet2".
  • And, you want to copy "B1" and "B10" of "Sheet1" to the columns "A" and "AD" of "Sheet2".
  • In your Spreadsheet, there are no merged cells.

In this case, how about the following sample script?

Sample script:

function onEdit(e) {
  const range = e.range;
  const sheet = range.getSheet();
  if (sheet.getSheetName() != "Sheet1" || range.getA1Notation() != "H1" || !range.isChecked()) return;
  const srcRange = sheet.getRange("A3:G6");
  const [[b1], , , , , , , , , [b10]] = sheet.getRange("B1:B10").getValues();
  const values = [b1, ...srcRange.getValues().flat(), b10];
  const dstSheet = e.source.getSheetByName("Sheet2");
  dstSheet.appendRow(values);
  range.uncheck();
  // srcRange.clearContent();
}
  • This script is automatically run with the simple trigger of OnEdit trigger. So, when you use this script, please check the checkbox of "H1" of "Sheet1". By this, the script is run. When you directly run the script, an error like Cannot read property 'range' of undefined occurs. Please be careful about this.
  • If you want to clear the source range after the values were copied, please use srcRange.clearContent(); .

Note:

  • This sample script is for your sample Spreadsheet. So, when you changed your Spreadsheet, this script might not be able to be used. Please be careful about this.

Reference:

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