Search code examples
google-apps-scriptgoogle-sheetsgoogle-sheets-api

Copy multiple cells across multiple rows and columns, and paste to single row, with cells pasted sequentially


I have a data-set in Google Sheets that needs to be rearranged. I have data across multiple rows and columns that I would like to be pasted onto a single row, sequentially, following a logic of left to right per row, then proceed downwards for each row.

The amount of initial rows varies per entry, so ideally, I would highlight the number of rows and the output would respond accordingly. The quantity of columns containing values will be 4 cells per row.

An example of the initial state

Initial State

An example of the desired state

enter image description here

I would like to write this myself, as the logic is not complex, but I have never programmed with JavaScript, which, to my understanding, is the only language for Google Apps Scripts. Is there a way to write these scripts in Python, Java, and or C, and use them with Google Sheets? Those are the languages I'm more familiar with.


Solution

  • function myFunck() {
      const ss = SpreadsheetApp.getActive();
      const sh = ss.getSheetByName("Sheet0");
      const vs = sh.getRange(1, 1, sh.getLastRow(), sh.getLastColumn()).getValues().flat();
      const osh = ss.getSheetByName("Sheet1");
      osh.clearContents();
      osh.getRange(1, 1, 1, vs.length).setValues([vs]);
    }