M
M
Michael2021-02-18 16:30:06
Google Sheets
Michael, 2021-02-18 16:30:06

How to add data from this tab to a specific tab when creating a new tab in a Google spreadsheet?

Essence of a question: there is a table with the data (availability in a warehouse). Every day, a tab with data is created (income, expense by warehouse). How to synchronize the main table with the data in the created tab automatically. data should be added to existing rows (increase or decrease the number) and when creating a trail tab, the data was added again to the existing main table

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Grigory Boev, 2021-02-19
@ProgrammerForever

As one of the options: form a formula and substitute it when changing the structure of the table (you need to set up a trigger).
Formula of this kind:
=arrayformula({'Лист2'!1:1000;'Лист3'!1:1000})
Code for forming the formula:

function onChange() {
  const targetSheetName = "Лист1"; // Сюда вставляем значения
  const targetSheetRange = "A1";   // Сюда вставляем значения
  const dataRange = "1:1000";      // Диапазон данных извлекаемых из каждого листа

  let as = SpreadsheetApp.getActiveSpreadsheet();
  let sheets = as.getSheets();
  
  let outFormula = "=arrayformula({";
  let sheetNames = sheets
                    .map(sheet=>sheet.getName())
                    .filter(sheetName=>sheetName!=targetSheetName)
                    .map(sheetName=>"'"+sheetName+"'!"+dataRange)
                    .join(";");
  outFormula+=sheetNames;
  outFormula+="})";

  as.getSheetByName(targetSheetName).getRange(targetSheetRange).setFormula(outFormula);
}

602ff2cf505aa733444592.png
Demo table

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question