0
votes

Hy, I am using Google Sheet which is used to calculate the commission of agents. I am not familiar with the Google Script. Here is the link of the Sheet

enter link description here

I need a script that enables me to copy data from Calculator range B3:B5, D3:D5, and F3:F5 on the RawData sheet.

The script should enable me to copy data every time when I will press the "Submit" button and every time the record should be on the next empty row. I hope this makes sense.

3
Did this answer worked for you? this one created the Submit button in your file.soMario

3 Answers

2
votes

Explanation:

The following script will copy the data from the Calculator sheet; ranges B3:B5, D3:D5 and F3:F5 to the next available row of the RawData sheet when you click a button called Submit. In your case, I created the button under the Scripts menu as you can see in the screenshot below. I already implemented the solution for you in the shared document, therefore you just have to try it out.


Solution:

function copyToRawData() {
  
  const ss = SpreadsheetApp.getActive();
  const cal_sh = ss.getSheetByName('Calculator');
  const raw_sh = ss.getSheetByName('RawData');
  const ranges = ['B3:B5','D3:D5','F3:F5'];
  const values = [];
  ranges.forEach(r=>values.push(...cal_sh.getRange(r).getValues().flat()));
  raw_sh.getRange(raw_sh.getLastRow()+1,1,1,values.length).setValues([values]);
  
}

function onOpen() {
  SpreadsheetApp.getUi()
  .createMenu('Menu')
  .addItem('Submit', 'copyToRawData')
  .addToUi();
}

Result:

Here is the submit button that you need to press in order to perform this operation.

result

2
votes

Code sample:

function copyData() {
  const ss = SpreadsheetApp.getActive();
  const sourceSheet = ss.getSheetByName("Calculator");
  const targetSheet = ss.getSheetByName("RawData");
  const rangeList = sourceSheet.getRangeList(['B3:B5', 'D3:D5', 'F3:F5']);
  const values = rangeList.getRanges().map(range => range.getValues()).flat(2);
  targetSheet.appendRow(values);
}
1
votes

You do not need a script for that. Please use the following formula:

=TRANSPOSE(flatten({ Calculator!B3:B5;Calculator!D3:D5;Calculator!F3:F5}))

enter image description here

Please remember that the data will always be updated.

Functions used: