将单元格值复制到另一个 sheet 以获得匹配值

Copy cell value to another sheet for matching value

我有一个包含两个 sheet 的传播sheet:“数据库”和“附录”。一个有更改记录,另一个作为数据库。

“数据库”包含以下列:

“Adendas”有:

观察:

更改已添加到 'Adendas' sheet,并指定了起始日期。当日期匹配今天时,应该根据更改类型复制相应的信息,从 'Adendas' 到 'Data Base'

中的匹配名称

例如,今天04/25/2022,它应该将车辆信息从Adendas中的Column P移动到Column MName2 的数据库,然后将真值应用到 Adenda 的 Column R

中的确认框

结果应如下所示:

数据库 附录

经过大量搜索和调查,我设法创建了以下适用于日期和复选框部分的代码,但我无法设法将更改应用于匹配的名称值,因为它正在复制它,但是在随机单元格。

function parse_worker(employee) {
    /* Given a row from the spreadsheet, parse the data into a javascript object. */

    if (typeof(employee) == 'undefined') {
      return null;
    }

    let trabajador = {};

    trabajador.nombre = employee[3];
    trabajador.tipo = employee[12];
    trabajador.location = employee[13];
    trabajador.hours = employee[14];
    trabajador.vehicle = employee[15];
    trabajador.fecha = employee[16];
    trabajador.cambio = employee[17];

    return trabajador;
};

function are_dates_equal(date1, date2) {
    /* Returns true if the two dates are equal, false otherwise. 
    
    It only compares the day, the month and the year. Time is not considered.
    */

    return date1.getUTCDate() == date2.getUTCDate() && date1.getUTCMonth() == date2.getUTCMonth() && date1.getUTCFullYear() == date2.getUTCFullYear();
}

function try_to_copy_data() {
    /* Iterate over the rows in the spreadsheet and make the corresponding change if it's time to do it. */

    var app = SpreadsheetApp;
    var spreadsheet = app.getActiveSpreadsheet();
    var sourceSheet = spreadsheet.getSheetByName('Adendas');
    var sourceRows = sourceSheet.getRange(2, 1, sourceSheet.getLastRow() - 1, sourceSheet.getLastColumn()).getValues();
    var targetSheet = spreadsheet.getSheetByName('Data Base');
    var targetRows = targetSheet.getRange(2, 1, targetSheet.getLastRow() - 1, targetSheet.getLastColumn()).getValues();


    sourceRows.forEach(function(row, i) {
        /* Copy's the data to the given employee if it's time to do it and the change type matches. */

        // Exit the function if the employee has no date.
        let is_date_set = row[16] !== '';
        if (!is_date_set) {
            return;
        }

        let worker = parse_worker(row);
        var now = new Date();

        Logger.log(now.toLocaleDateString('es-ES', {timeZone: 'UTC'}));
        Logger.log(worker.fecha.toLocaleDateString('es-ES', {timeZone: 'UTC'}));

        // Exit the function if is not time to make the change
        if (!are_dates_equal(now, worker.fecha)) {
            return;
        }

        // Exit the function if change has already made
        if (worker.cambio === true) {
            return;
        }

        if (are_dates_equal(now, worker.fecha) && worker.tipo === "Vehículo"){
            targetSheet.getRange(i+2,[13]).setValue(worker.vehicle);
            sourceSheet.getRange(i+2,[18]).setValue(true);
        } else if (are_dates_equal(now, worker.fecha) && worker.tipo === "Centro de Trabajo"){
            targetSheet.getRange(i+2,[11]).setValue(worker.vehicle);
            sourceSheet.getRange(i+2,[18]).setValue(true);
        } else if (are_dates_equal(now, worker.fecha) && worker.tipo === "Horas Contrato"){
            targetSheet.getRange(i+2,[12]).setValue(worker.vehicle);
            sourceSheet.getRange(i+2,[18]).setValue(true);
        } else {
            Logger.log("Error! Not a valid change.");
        }

    })
}
try_to_copy_data();

我的想法是使用关于如何自动复制值然后使其适应定位的常识,但我什至不知道如何去做。

编辑:感谢 Yuri 的帮助,最终的工作代码

function parse_worker(employee) {
    /* Given a row from the spreadsheet, parse the data into a javascript object. */

    if (typeof(employee) == 'undefined') {
      return null;
    }

    let trabajador = {};

    trabajador.nombre = employee[3];
    trabajador.tipo = employee[12];
    trabajador.location = employee[13];
    trabajador.hours = employee[14];
    trabajador.vehicle = employee[15];
    trabajador.fecha = employee[16];
    trabajador.cambio = employee[17];

    return trabajador;
};

function are_dates_equal(date1, date2) {
    /* Returns true if the two dates are equal, false otherwise. 
    
    It only compares the day, the month and the year. Time is not considered.
    */

    return date1.getUTCDate() == date2.getUTCDate() && date1.getUTCMonth() == date2.getUTCMonth() && date1.getUTCFullYear() == date2.getUTCFullYear();
}

function try_to_copy_data() {
    /* Iterate over the rows in the spreadsheet and make the corresponding change if it's time to do it. */

    var app = SpreadsheetApp;
    var spreadsheet = app.getActiveSpreadsheet();
    var sourceSheet = spreadsheet.getSheetByName('Adendas');
    var sourceRows = sourceSheet.getRange(2, 1, sourceSheet.getLastRow() - 1, sourceSheet.getLastColumn()).getValues();
    var targetSheet = spreadsheet.getSheetByName('Data Base');
    var targetRows = targetSheet.getRange(2, 1, targetSheet.getLastRow() - 1, targetSheet.getLastColumn()).getValues();
    var targetNames = targetRows.map(e => e[3]);



    sourceRows.forEach(function(row, i) {
        /* Copy's the data to the given employee if it's time to do it and the change type matches. */

        // Exit the function if the employee has no date.
        let is_date_set = row[16] !== '';
        if (!is_date_set) {
            return;
        }

        let worker = parse_worker(row);
        var now = new Date();
        var row_index = targetNames.indexOf(worker.nombre);


        Logger.log(now.toLocaleDateString('es-ES', {timeZone: 'UTC'}));
        Logger.log(worker.fecha.toLocaleDateString('es-ES', {timeZone: 'UTC'}));

        // Exit the function if is not time to make the change
        if (!are_dates_equal(now, worker.fecha)) {
            return;
        }

        // Exit the function if change has already made
        if (worker.cambio === true) {
            return;
        }

        if (are_dates_equal(now, worker.fecha) && worker.tipo === "Vehículo"){
            targetSheet.getRange(row_index+2,13).setValue(worker.vehicle);
            sourceSheet.getRange(i+2,[18]).setValue(true);
        } else if (are_dates_equal(now, worker.fecha) && worker.tipo === "Centro de Trabajo"){
            targetSheet.getRange(row_index+2,11).setValue(worker.location);
            sourceSheet.getRange(i+2,[18]).setValue(true);
        } else if (are_dates_equal(now, worker.fecha) && worker.tipo === "Horas Contrato"){
            targetSheet.getRange(row_index+2,12).setValue(worker.hours);
            sourceSheet.getRange(i+2,[18]).setValue(true);
        } else {
            Logger.log("Error! Not a valid change.");
        }

    })
}
try_to_copy_data();

大概应该是这样的:

function try_to_copy_data() {

  var now = new Date();

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sourceSheet = ss.getSheetByName('Adendas');
  var sourceRows = sourceSheet.getDataRange().getValues().slice(1);
  var targetSheet = ss.getSheetByName('Data Base');
  var [header, ...targetRows] = targetSheet.getDataRange().getValues();
  var targetNames = targetRows.map(e => e[3]);

  sourceRows.forEach((row, i) => {

    let worker = parse_worker(row);
    if (worker.ok == true) return;
    if (!are_dates_equal(now, worker.date)) return;

    var row_index = targetNames.indexOf(worker.name);

    if (worker.location != '') targetRows[row_index][10] = worker.location;
    if (worker.hours != '')    targetRows[row_index][11] = worker.hours;
    if (worker.vehicle != '')  targetRows[row_index][12] = worker.vehicle;

    sourceSheet.getRange('R' + (i+2)).check();
  })

  var table = [header, ...targetRows];
  targetSheet.clearContents()
    .getRange(1, 1, table.length, table[0].length)
    .setValues(table);
}

function parse_worker(row) {
  let worker = {
    name:     row[3],
    type:     row[12],
    location: row[13],
    hours:    row[14],
    vehicle:  row[15],
    date:     row[16],
    ok:       row[17],
  }
  return worker;
}

function are_dates_equal(date1, date2) {
  return date1.getUTCDate() == date2.getUTCDate() &&
    date1.getUTCMonth() == date2.getUTCMonth() &&
    date1.getUTCFullYear() == date2.getUTCFullYear();
}