N
N
Nik322020-01-25 19:41:04
Google Apps Script
Nik32, 2020-01-25 19:41:04

Copy data from one sheet of a table to another table per sheet to the last empty row?

Hello, please tell me how to make it copied from a table to a table, for example, from the "Employee1" table, to the "Data" table, this is possible, thank you for your help, that is, if a person has access only to the "Employee1" table, but there will be no access to the "Data" table, will the script work in this case? Here is the code that copies from sheet to sheet.

/**
 *
 */
function onOpen() {
  SpreadsheetApp.getUi()
    .createMenu('Custom menu')
    .addItem('Copy to logs', 'userActionsCopyToLogs')
    .addToUi();
}

/**
 *
 */
function userActionsCopyToLogs() {
  var headers = ['Name', 'Number'];

  var from = SpreadsheetApp.getActiveSheet();

  if (from.getName() !== 'Sheet1') {
    SpreadsheetApp.getActive().toast('Activate a range on "Sheet1"');
    return;
  }

  var fromValues = from.getDataRange().getValues();

  var fromHeaders = fromValues[0].map(function(h) {
    return headers.indexOf(h);
  });

  var activeRange = SpreadsheetApp.getActiveRange();
  var rowStart = activeRange.getRow();
  var rowEnd = activeRange.getLastRow();
  var fromData = fromValues
    .filter(function(row, i) {
      return i >= rowStart - 1 && i <= rowEnd - 1;
    })
    .map(function(row) {
      return row.filter(function(_, j) {
        return fromHeaders[j] > -1;
      });
    });

  var to =
    SpreadsheetApp.getActive().getSheetByName('Logs') ||
    SpreadsheetApp.getActive().insertSheet('Logs');

  to.getRange(to.getLastRow() + 1, 1, fromData.length, fromData[0].length)
    .setValues(fromData)
    .activate();
}

The code is not mine here is a link to it

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Ivanov, 2020-01-25
@oshliaer

Unfortunately, most likely an accurate answer with a solution to such a question is beyond the scope of public support. But no one cancels the theory.
In this case, you need to create two scripts:
There are other reliable hacks that work without the user even knowing about them.
In any case, dig towards Google Apps Script Web Apps. This should help. You don't need any deprivation of code, except for sending a request to the server and receiving data on the server. Everything else is already there.
Sincerely.

S
Shohruh Shaimardonov, 2020-01-27
@joeberetta

if a person has access only to the "Employee1" table, and no access to the "Data" table, will the script work in this case?

No, in this case the script will be run on behalf of the user, and it will have access only to those areas that the user himself is allowed to access.
Also, scripts that need "special" privileges will require access to run before the first run.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question