简体   繁体   中英

Writing data to master Google Spreadsheet from multiple other sheets; am overwriting results of each feeder sheet instead of getting all results

Situation: Aggregating information from several feeder Trixes and trying to populate the results from feeders to one sheet of a master Trix.

Problem: I want to write the results of each feeder trix (gathered with get_data_from_feeders() function below), to the same sheet in a Master Trix. However, each feeder array overwrites the previous feeder results, so only the last feeder trix results are shown in the Master Trix instead of all three (write_data(data) function below).

Questions:

1) How can I make each feeder set of results append the previous feeder trix results instead of overwriting them? [This will be run monthly, so each month it's okay to overwrite what had been on the sheet from last month]

2) How can I sort all of the results (I think I use .sort() but am unsure where to call it)?

Current code:

function get_data_from_feeders() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var feeders = ss.getRangeByName('spokes').getValues();
  for (var i = 0; i <= feeders.length; i++) {
    var feeder_id = feeders[i][0];
    var feeder_open = SpreadsheetApp.openById(feeder_id);
    var dataPull = feeder_open.getRangeByName('dataPull').getValues(); 

  write_data(dataPull); 
  }
}

function write_data(data) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var ws = ss.getSheetByName("Forecast_Aggregation");
  var dataDump = ss.getRangeByName("data_dump");

// I think the next line is where my code is incorrect - each feeder goes back to same cell instead of to the next blank row (hence, overwriting instead of appending)

  ws.getRange(dataDump.getRowIndex(), 2, data.length, 20).setValues(data);
}

Thanks for your help!

Perhaps try:

function write_data(data) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var ws = ss.getSheetByName("Forecast_Aggregation");
  var dataDump = ss.getRangeByName("data_dump").getRow();
  var nextRow = Math.max(dataDump, ws.getLastRow())+1;
  ws.getRange(nextRow, 2, data.length, 20).setValues(data);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM