SK360 Kiry
SK360 Kiry

Reputation: 5

apps script how copy selected cells in row from sheet to target cells in row of other sheet

I am new to google apps script. I have source sheet (Column A: No, B: Name, C: Sex, D: Age, E:Type, F: Salary, G: Status (value yes/no) and destination sheet (Column A: Name, B: Sex, C: Salary). If a column G value == yes in source sheet, I want to copy value of row in column name, sex, salary to the last row of destination sheet. Thanks everyone

I am stuck, I don't know how to start the code.

My Fake Data:

No Name Sex Age Type Salary Status(yes/no)
A2 B2 C2 D2 E2 F2 yes
A3 B3 C3 D3 E3 F3 no
A4 B4 C4 D4 E4 F4 yes
A5 B5 C5 D5 E5 F5 no
A6 B6 C6 D6 E6 F6 yes
A7 B7 C7 D7 E7 F7 no
A8 B8 C8 D8 E8 F8 yes
A9 B9 C9 D9 E9 F9 no
A10 B10 C10 D10 E10 F10 yes

Output:

Name Sex Phone Email Salary
B2 C2 - - F2
B4 C4 - - F4
B6 C6 - - F6
B8 C8 - - F8
B10 C10 - - F10

Upvotes: 0

Views: 60

Answers (1)

Cooper
Cooper

Reputation: 64040

This get's the data from the src sheet and filters out all of the no's and maps cols b,c and f to a,b,c and appends them into the des sheet

function myfunk01() {
  const ss = SpreadsheetApp.getActive();
  const ssh = ss.getSheetByName("src");
  const dsh = ss.getSheetByName("des");
  const sr = 2;
  const dvs = ssh.getRange(sr,1,ssh.getLastRow() - sr + 1,ssh.getLastColumn()).getValues().filter(([a,b,c,d,e,f,g]) => g == "yes").map(([a,b,c,d,e,f,g]) => [b,c,-,-,f]);
  dsh.getRange(dsh.getLastRow() + 1,1,dvs.length,dvs[0].length).setValues(dvs);
}

My Fake Data:

No Name Sex Age Type Salary Status(yes/no)
A2 B2 C2 D2 E2 F2 yes
A3 B3 C3 D3 E3 F3 no
A4 B4 C4 D4 E4 F4 yes
A5 B5 C5 D5 E5 F5 no
A6 B6 C6 D6 E6 F6 yes
A7 B7 C7 D7 E7 F7 no
A8 B8 C8 D8 E8 F8 yes
A9 B9 C9 D9 E9 F9 no
A10 B10 C10 D10 E10 F10 yes

Output:

Name Sex Salary
B2 C2 F2
B4 C4 F4
B6 C6 F6
B8 C8 F8
B10 C10 F10

Upvotes: 0

Related Questions