Home > Software engineering >  Google Sheets, rearrange data to new rows
Google Sheets, rearrange data to new rows

Time:05-10

Second (and hopefully last) google sheets question today and forever. As I am importing my data from a management system to another via google sheets, I am having some difficulty in following the new system's method of importing data. So currently my sheet looks like this:

A B C D E F G H I J K L M N
1 ID First Name Email Address Phone Email ID (Email IDs) Number (Contact Numbers) Is Primary (Email IDs) Is Primary Phone (Contact Numbers) Is Primary Mobile (Contact Numbers) Mobile No Company Name Link Document Type (Links) Link Name (Links) Link Title (Links)
2 FERTICLINIC HOME CARE CENTRE L.L.C . FERTICLINIC HOME CARE CENTRE L.L.C . [email protected] 971 2 1234567 [email protected] 1 971 50 12345678 1 FERTICLINIC HOME CARE CENTRE L.L.C . Customer 1 FERTICLINIC HOME CARE CENTRE L.L.C . 1 FERTICLINIC HOME CARE CENTRE L.L.C .

What I want to do is have numbers on column D and J to show at F (second number would be on a new row) So the end result would look like this

A B C D E F G H I J K L M N
1 ID First Name Email Address Phone Email ID (Email IDs) Number (Contact Numbers) Is Primary (Email IDs) Is Primary Phone (Contact Numbers) Is Primary Mobile (Contact Numbers) Mobile No Company Name Link Document Type (Links) Link Name (Links) Link Title (Links)
2 FERTICLINIC HOME CARE CENTRE L.L.C . FERTICLINIC HOME CARE CENTRE L.L.C . [email protected] 971 2 1234567 [email protected] 971 50 12345678 (From J) 1 971 50 12345678 1 FERTICLINIC HOME CARE CENTRE L.L.C . Customer 1 FERTICLINIC HOME CARE CENTRE L.L.C . 1 FERTICLINIC HOME CARE CENTRE L.L.C .
3 971 2 1234567 (From D)

So basically the script would copy data from "J" to "F" then make a new row and copy data from "D" to "F" on the new row.

I hope this makes sense, any assistance is appreciated. Thanks!

CodePudding user response:

In your situation, how about the following sample script?

Sample script:

Please set the source and destination sheet names.

function myFunction() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var srcSheet = ss.getSheetByName("Sheet1"); // Please set the source sheet name.
  var dstSheet = ss.getSheetByName("Sheet2"); // Please set the destination sheet name.

  var [, ...values] = srcSheet.getDataRange().getValues();
  var res = values.flatMap(r => {
    r[5] = r[9];
    var temp = Array(r.length).fill(null);
    temp[5] = r[3];
    return [r, temp];
  });
  dstSheet.getRange(1, 1, res.length, res[0].length).setValues(res);
}
  • When this script is run, each row is put as 2 rows. The value of column "J" is put to the column "F". And, the value of column "D" is put to the column "F".

Reference:

  • enter image description here

    you just have to enable iterative calculation in the settings.

    File> Settings> Calculation> Iterative Calculation Enable this option and save

  • Related