T
T
Tyschenko2020-10-01 07:58:01
Google Apps Script
Tyschenko, 2020-10-01 07:58:01

How to link to previous sheet in Google Sheets?

Good afternoon. Tell me, please, how can I refer to the previous sheet (not manually select it, but automatically)?

When creating a new sheet, I want it to take data from the previous sheet (from the same cells).

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Stoyanov, 2020-10-01
@Tyschenko

Get the index of the current sheet:

const ss = SpreadsheetApp.getActiveSpreadsheet()
const sheet = ss.getActiveSheet();
const index = sheet.getIndex();

Then get the previous sheet by index Do you need a formula?
ss. getSheets()[index - 2];

function getListPrev(range) {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getActiveSheet();
  const index = sheet.getIndex();
  
  const formula = sheet.getActiveRange().getFormula();
  range = formula.match(/getListPrev\(([^\)]*?)\)/i)[1].trim();
  
  const sheet_prev = (ss.getSheets()[index - 2] || null);
  
  if (!sheet_prev)
    throw new Error('Нет предыдущего листа!');
  
  return sheet_prev
  .getRange(range)
  .getValues();
}

5f76e926c261d767962334.png
---
Maxim Stoyanov (stomaks), developer of Google Apps Script .
g-apps-script.com
stomaks.me

A
Alexander, 2020-10-01
@ForestAndGarden

Write the name of the previous sheet in cell A1, refer to it in the formulas.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question