Home > database >  Google Sheet Scripts - Import values down columns rather than across the row
Google Sheet Scripts - Import values down columns rather than across the row

Time:09-06

I've got a script gets all data from spreadsheets named "PURCHASING SHEET" which grabs certain columns and puts them into an array of arrays and imports them into my "MASTER SHEET" to hold everything in one place.

Every time I run the script it lists them across the row rather than down the column, how can I avoid/ change doing this?

also would it be possible to have the run in the background and get live updates or when I run it, it appends to the bottom rather than clearing the sheet and starting a fresh?

function getPurchasingSheetId() {
 var myFiles = DriveApp.searchFiles('"me" in owners');
 var ids = [];
 while(myFiles.hasNext()) {
   var file = myFiles.next();
   if(file != null) {
     if(file.getName() == 'PURCHASING SHEET'){
      ids.push(file.getId());
     }
   }
 }
 getSKUdata(ids)
}

function getSKUdata(ids){
  var skuList = []
  for (id of ids){
    var ss = SpreadsheetApp.openById('id').getSheetByName('PURCHASES');
    var rowValues = ss.getRange(5,1,ss.getLastRow(),7).getValues();
    for (row of rowValues){
      if (row[1] != ''){
        skuList.push([row[0],row[1],row[6]])
      }
    }
  }
  writeData(skuList)
}

function writeData(list){
  var ss = SpreadsheetApp.getActive();
  var dest = ss.getSheetByName('2023')
  dest.getRange(dest.getLastRow() 1,1,1,list.length).setValues([list])
}

CodePudding user response:

Change this

dest.getRange(dest.getLastRow() 1,1,1,list.length).setValues([list]);

To this

dest.getRange(dest.getLastRow() 1,1,list.length,list[0].length).setValues(list);

list is already and array of arrays from this command

skuList.push([row[0],row[1],row[6]]);
  • Related