Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
219 views
in Technique[技术] by (71.8m points)

Synchronous Scrolling across sheets (tabs) in a Google Sheets Spreadsheet

I am new to Google Sheets, Apps Scripts and JavaScript so kindly bare with me.

I am particularly interested in Google Sheets due to its ability to control user access to specific ranges for data entry purposes.

I have information that I would like split across different sheets in a spreadsheet. As there is a lot of information with details that can be categorized, I am planning to separate the information across different sheets/tabs per category for ease of use.

Due to the expected large number of rows, I wanted to be able to synchronously scroll across these category sheets also for ease of use. I managed to do the same using Macros on Excel, please see this excel file to have an idea of what I am intending to do.

Basically when a cell (or even row) is selected in the active category sheets, the same row is selected in all the other inactive category sheets. Alternatively, when the user selects a category sheet, it should check which row is selected in the previously selected category sheet and select the same row in the newly active sheet.

I was playing with Apps Script but wasn't able to set the selection of an inactive sheet. Similarly, when selecting another category sheet, I was unable to get the selected range from the previously selected category sheet - getSelection() and getActiveRange() always return selection of the current active sheet.

Is there any way I can achieve this? Are there alternative solutions anyone can suggest to reach a similar target? For example I was also looking at the sidebar but that reduces the number of columns viewable ... wanted to minimize horizontal scrolling as that is the main intention of splitting the data between sheets to begin with.

I will greatly appreciate any feedback.

Thanks in advance.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If I understand you correctly, you want to programmatically scroll all sheets to the same row as the active range.

If that's the case, you can just look through all sheets and set the corresponding row (using the rowIndex from the active range) as the active range, using Sheet.setActiveRange(range), as shown here:

function scrollAllSheets() {
  const rowIndex = SpreadsheetApp.getActiveRange().getRow();
  const sheets = SpreadsheetApp.getActive().getSheets();
  sheets.forEach(sheet => {
    const row = sheet.getRange(rowIndex, 1, 1, sheet.getLastColumn());
    sheet.setActiveRange(row);
    //SpreadsheetApp.flush();
  });
}

If you want the script to run when a new cell is clicked, use an onSelectionChange(e) trigger. For that, just change your function name to onSelectionChange and, optionally, use the event object (argument e):

function onSelectionChange(e) {
  const rowIndex = e.range.getRow();
  const sheets = e.source.getSheets();
  sheets.forEach(sheet => {
    const row = sheet.getRange(rowIndex, 1, 1, sheet.getLastColumn());
    sheet.setActiveRange(row);
  });
}

Note:

  • I don't think it's necessary, but in order to make sure the spreadsheet selection is updated for all sheets, you might find SpreadsheetApp.flush() useful. Uncomment it from the code above if it's not working.
  • Using RangeList is not an option here, since it refers to a list of ranges from the same sheet.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...