melwyn mendonca
melwyn mendonca

Reputation: 33

Getting values from different cell and set via googlesheet script

I am new to script

I want to get values from different cells and copy them to another sheet via app script when the user presses the button.

Is it possible?

Here is the Test sheet link https://docs.google.com/spreadsheets/d/1vgd-5jPfjjil2iNcrcfO4awZupqL7m8CofIUMyCWkN8/edit#gid=352097475

So I want the highlighted cell value enter image description here

When the user clicks the Add to sheet2 I need to display the cell value from sheet1 to the respective cell in Sheet2 enter image description here

Upvotes: 3

Views: 75

Answers (1)

Yuri Khristich
Yuri Khristich

Reputation: 14502

Try this:

function myFunction() {

  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const s1 = ss.getSheetByName("Sheet1");
  const s2 = ss.getSheetByName("Sheet2");

  const data = s1.getRange('B2:H10').getValues();

  var quantity       = data[0][0];
  var weight         = data[1][0];
  var label          = data[2][0];
  var sheet_name     = s1.getName();
  var packaging_cost = data[8][3];
  var price          = data[8][6]
  
  const row = [
    quantity,
    weight,
    label,
    sheet_name,
    packaging_cost,
    price
  ];
  
  s2.getRange("A3:F3").setValues([row]);
}

Probably you want to use

s2.appendRow(row);

instead of the last line in the code. It will append a new row on Sheet2 every time you run the code.

Upvotes: 3

Related Questions