Reputation: 10638
Using Google Sheets I am trying to make a copy button that once clicked it should launch a script code in order to copy some values in a specific column (for example 20 specific cells starting from a specific row) and paste them to the next empty column in another sheet (sheet 2) starting from a specific row.
Each time the button is pressed it copies the results to the next empty column in sheet 2 so it doesn't copy over the previous pasted result.
Just the value or text from these cells on sheet 1: C4,C5,C6,C7,C8,C9,C10,C11,C12,C13,C14,C15,C16,C17,C18,C19,C20,C21,C22,C23
Into corresponding cells on sheet 2: F11:F30
C4 will go into F11, C5 into F12, and so on ..... and finally C23 into F30
Next time the copy button is clicked, again values from C4:C23 in sheet 1 will be copied to the next emtpy column in sheet 2, that is, G11:G30.
Upvotes: 0
Views: 2355
Reputation: 27348
Solution:
You can find the explanation in the comments:
function myFunction() {
const ss = SpreadsheetApp.getActive();
// get sheet objects for Sheet1 and Sheet2
const sh1 = ss.getSheetByName('Sheet1'); // use your sheet name for sheet1
const sh2 = ss.getSheetByName('Sheet2'); // use your sheet name for sheet2
// get the data from Sheet1 C4:C23
const data = sh1.getRange('C4:C23').getValues();
// copy the data to Sheet2 starting from row 11 after the last column with content
sh2.getRange(11,sh2.getLastColumn()+1,data.length,1).setValues(data);
}
References:
Upvotes: 3