How do I get the last row of multiple sheets and put them together into a new row in an Excel VBA or Google Apps Script?

Asked 1 years ago, Updated 1 years ago, 105 views

Please check the sheet below first.

https://docs.google.com/spreadsheets/d/1AlwgQLEZoZz_vWz0ZqKld528ViLAl8x2nR4SZz0VoHA/edit#gid=0

There is an Excel file that records the monthly inventory of each product in the form of ↑.
Is there a way to automatically put together the latest monthly inventory of each item into one table as follows?

https://docs.google.com/spreadsheets/d/1AlwgQLEZoZz_vWz0ZqKld528ViLAl8x2nR4SZz0VoHA/edit#gid=1267878017

Points to Note
·The rows for the latest monthly inventory of each product sheet are different depending on the sheet.
·The latest monthly inventory of each product sheet will be added to the last column.

There are more than 100 items, so it is very troublesome to put them together manually...
Is there any way to automatically process it with Excel VBA or GAS?

I look forward to your kind cooperation.

google-apps-script vba excel google-spreadsheet

2022-09-30 19:34

1 Answers

It worked as follows.
Please check the details in the comments.
Also, since there was nothing I did this time, I printed it on a specific sheet, but
In order to put it into operation, I think it is necessary to create a separate sheet and print it out when the month changes.

var OUTPUT_SHEET_NAME="For Output"
function myFunction() {
  vars = SpreadsheetApp.openById(SpreadsheetApp.getActiveSpreadsheet().getId());

  var sheets=ss.getSheets();
  var outputSheet=ss.getSheetByName(OUTPUT_SHEET_NAME);

  for(i=0;i<sheets.length;i++){
    varsheet=sheets[i];
    variable=sheet.getName();
    if(title===OUTPUT_SHEET_NAME){
      continue;
    }
//    Logger.log(title);
    var lastRowNumber=sheet.getLastRow();
    var lastColumnNumber=sheet.getLastColumn();

    // Obtain number of volumes
    // 3 is the third row, 1 is the first row, lastRowNumber-2 is the last row minus two rows of the header, and 1 is the first row.
    var numbers = sheet.getRange(3,1, lastRowNumber-2,1).getValues();
//    Logger.log(number);

    // Acquire inventory
    var values = sheet.getRange(3, lastColumnNumber, lastRowNumber-2,1).getValues();
//    Logger.log(values);

    // Create output values (Title, Volume, Inventory)
    var result = [ ]
    for(j=0;j<number.length;j++){
      result.push([title, numbers[j][0], values[j][0]]);
    }
//    Logger.log(result);

    // Output to Output Sheet
    var outputSheetLastRowNumber=outputSheet.getLastRow();
    var outputRange=outputSheet.getRange(outputSheetLastRowNumber+1,1,result.length,3);
    outputRange.setValues(result);
  }
}

Reference: Official Documents ↓
https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet-app


2022-09-30 19:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.