Enhance GS Script to Update specific column values to another Sheet (specific columns) if row ID matches in both sheet

huangapple go评论60阅读模式
英文:

Enhance GS Script to Update specific column values to another Sheet (specific columns) if row ID matches in both sheet

问题

  1. 在GS脚本中,我正在尝试从"工资单"表格更新到"日志"表格。
  2. 根据条件,如果工资单表格A3:A匹配日志表A2:A,在工资单表格中的前两行是标题,而在日志表中的第一行是标题。
  3. 只有指定的列/单元格需要更新。
  4. 给出的示例代码正在运行,但非常慢。

这些是要更新的确切列:

工资单到日志

B到B

G到G

I到J

J到K

K到M

L到W

M到X

N到Y

O到AA

P到AC

Q到AL

R到AN

S到AO

T到AQ

U到AS

V到O

W到P

X到Q

Y到U

Z到AE

AA到AF

AB到AG

AC到AK

AD到AU

AE到AV

AF到AW

AG到BA


谢谢 Enhance GS Script to Update specific column values to another Sheet (specific columns) if row ID matches in both sheet

英文:
  1. In GS script I am trying to update the values from the "Payroll" Sheet to the "Log" Sheet.
  2. Based on the criteria, If Payroll Sheet A3:A matches to Log Sheet A2:A
    in the Payroll sheet first 2 Rows are headers, and in Log Sheet first row is a header.
  3. Only specified columns/cells must be updated.
  4. given sample code is working, But VERY SLOW.
function updateLogSheet() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var payrollSheet = ss.getSheetByName("Payroll");
  var logSheet = ss.getSheetByName("Log");

  // Get the Emp.code from A1 of the Payroll sheet
  var empCode = payrollSheet.getRange("A1").getValue();

  // Get the values in the Date list from A3:A of the Payroll sheet
  var logIds = payrollSheet.getRange("A3:A").getValues().flat().filter(Boolean);

  var logValues = logSheet.getRange("A2:AH").getValues();
  logValues = logValues.filter(function (x) {
    return !(x.every(element => element === (undefined || null || '')))
  });

  for (var i = 0; i < logValues.length; i++) {

    for (var j = 0; j < logIds.length; j++) {
      if (logIds[j] === logValues[i][0]) {
        var payrollIndex = j + 3;
        if (logValues[i][2] == empCode && payrollIndex !== -1) {
          var rowIndex = i + 2; // Add 2 because the range starts at A2
          if (rowIndex != -1) {
            // Update the cells in the Log sheet
            logSheet.getRange(rowIndex, 2).setValue(payrollSheet.getRange("B" + payrollIndex).getValue()); // Date

            logSheet.getRange(rowIndex, 28).setValue(payrollSheet.getRange("AB" + payrollIndex).getValue()); // + symbol
           
            logSheet.getRange(rowIndex, 31).setValue(payrollSheet.getRange("AE" + payrollIndex).getValue()); // empty        
            
            logSheet.getRange(rowIndex, 34).setValue(payrollSheet.getRange("AH" + payrollIndex).getValue()); // Manual Hrs Adjust        

            logSheet.getRange(rowIndex, 7).setValue(payrollSheet.getRange("G" + payrollIndex).getValue()); // 1st ClockIN TIME 
            logSheet.getRange(rowIndex, 8).setValue(payrollSheet.getRange("H" + payrollIndex).getValue()); // 1-IN-Loc
            logSheet.getRange(rowIndex, 9).setValue(payrollSheet.getRange("I" + payrollIndex).getValue()); // 1-Activity
            logSheet.getRange(rowIndex, 11).setValue(payrollSheet.getRange("J" + payrollIndex).getValue()); // 1-Project         
            logSheet.getRange(rowIndex, 13).setValue(payrollSheet.getRange("K" + payrollIndex).getValue()); // 1-Note

            logSheet.getRange(rowIndex, 23).setValue(payrollSheet.getRange("L" + payrollIndex).getValue()); // 2-IN-TIME
            logSheet.getRange(rowIndex, 24).setValue(payrollSheet.getRange("M" + payrollIndex).getValue()); // 2-IN-Loc
            logSheet.getRange(rowIndex, 25).setValue(payrollSheet.getRange("N" + payrollIndex).getValue()); // 2-Activity
        logSheet.getRange(rowIndex, 27).setValue(payrollSheet.getRange("O" + payrollIndex).getValue()); // 2-Project
        logSheet.getRange(rowIndex, 29).setValue(payrollSheet.getRange("P" + payrollIndex).getValue()); // 2-Note     

        logSheet.getRange(rowIndex, 38).setValue(payrollSheet.getRange("Q" + payrollIndex).getValue()); // 3-IN-TIME        
        logSheet.getRange(rowIndex, 40).setValue(payrollSheet.getRange("R" + payrollIndex).getValue()); // 3-IN-Loc
        logSheet.getRange(rowIndex, 41).setValue(payrollSheet.getRange("S" + payrollIndex).getValue()); // 3-Activity
        logSheet.getRange(rowIndex, 43).setValue(payrollSheet.getRange("T" + payrollIndex).getValue()); // 3-Project
        logSheet.getRange(rowIndex, 44).setValue(payrollSheet.getRange("U" + payrollIndex).getValue()); // 3-Note

        logSheet.getRange(rowIndex, 15).setValue(payrollSheet.getRange("V" + payrollIndex).getValue()); // 1-OUT-TIME        
        logSheet.getRange(rowIndex, 16).setValue(payrollSheet.getRange("W" + payrollIndex).getValue()); // 1-OUT-Loc
        logSheet.getRange(rowIndex, 17).setValue(payrollSheet.getRange("X" + payrollIndex).getValue()); // 1-OUT-Activity
        logSheet.getRange(rowIndex, 21).setValue(payrollSheet.getRange("Y" + payrollIndex).getValue()); // 1-OUT-Note

        logSheet.getRange(rowIndex, 31).setValue(payrollSheet.getRange("Z" + payrollIndex).getValue()); // 2-OUT-TIME
        logSheet.getRange(rowIndex, 32).setValue(payrollSheet.getRange("AA" + payrollIndex).getValue()); // 2-OUT-Loc
        logSheet.getRange(rowIndex, 33).setValue(payrollSheet.getRange("AB" + payrollIndex).getValue()); // 2-OUT-Activity
        logSheet.getRange(rowIndex, 37).setValue(payrollSheet.getRange("AC" + payrollIndex).getValue()); // 2-OUT-Note

        logSheet.getRange(rowIndex, 47).setValue(payrollSheet.getRange("AD" + payrollIndex).getValue()); // 3-OUT-TIME
        logSheet.getRange(rowIndex, 48).setValue(payrollSheet.getRange("AE" + payrollIndex).getValue()); // 3-OUT-Loc
        logSheet.getRange(rowIndex, 49).setValue(payrollSheet.getRange("AF" + payrollIndex).getValue()); // 3-OUT-Activity
        logSheet.getRange(rowIndex, 53).setValue(payrollSheet.getRange("AG" + payrollIndex).getValue()); // 3-OUT-Note

           } 
        }
        payrollIndex = -1;
        break;
      }
    }
  }
}

Attached link and Sample Sheet image for reference

https://docs.google.com/spreadsheets/d/1UiwpNM2S0aeo4QinbEi-NqyPkGFVoZobqareeAOCsHs/edit?usp=sharing

Enhance GS Script to Update specific column values to another Sheet (specific columns) if row ID matches in both sheet

Enhance GS Script to Update specific column values to another Sheet (specific columns) if row ID matches in both sheet

these are the exact columns to be updated

Payroll to Log

B to B

G to G

I to J

J to K

K to M

L to W

M to X

N to Y

O to AA

P to AC

Q to AL

R to AN

S to AO

T to AQ

U to AS

V to O

W to P

X to Q

Y to U

Z to AE

AA to AF

AB to AG

AC to AK

AD to AU

AE to AV

AF to AW

AG to BA


thank you Enhance GS Script to Update specific column values to another Sheet (specific columns) if row ID matches in both sheet

答案1

得分: 1

以下是代码部分的中文翻译:

function sample() {
  // Ref: https://tanaikech.github.io/2022/07/04/converting-a1notation-to-gridrange-and-vice-versa-using-google-apps-script-without-any-scopes/
  const columnLetterToIndex_ = letter => [...letter.toUpperCase()].reduce((c, e, i, a) => (c += (e.charCodeAt(0) - 64) * Math.pow(26, a.length - i - 1)), -1);

  // 这些值来自于你的问题。
  const conversion = [["B", "B"], ["G", "G"], ["I", "J"], ["J", "K"], ["K", "M"], ["L", "W"], ["M", "X"], ["N", "Y"], ["O", "AA"], ["P", "AC"], ["Q", "AL"], ["R", "AN"], ["S", "AO"], ["T", "AQ"], ["U", "AS"], ["V", "O"], ["W", "P"], ["X", "Q"], ["Y", "U"], ["Z", "AE"], ["AA", "AF"], ["AB", "AG"], ["AC", "AK"], ["AD", "AU"], ["AE", "AV"], ["AF", "AW"], ["AG", "BA"]];

  const converted = conversion.map(e => e.map(f => columnLetterToIndex_(f)));
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const srcSheet = ss.getSheetByName("Payroll");
  const dstSheet = ss.getSheetByName("Log");
  const srcValues = srcSheet.getRange("A3:AG" + srcSheet.getLastRow()).getValues();
  const obj = srcValues.reduce((o, e) => (o[e[0]] = e, o), {});
  const dstRange = dstSheet.getRange("A2:BA" + dstSheet.getLastRow());
  const values = dstRange.getValues().map(e => {
    const ar = obj[e[0]];
    if (ar) {
      converted.forEach(([a, b]) => e[b] = ar[a] || null);
    }
    return e;
  });
  dstRange.setValues(values);
}

希望对你有所帮助。

英文:

I believe your goal is as follows.

  • You want to reduce the process cost of your script.

In this case, how about the following modification?

Modified script:

function sample() {
  // Ref: https://tanaikech.github.io/2022/07/04/converting-a1notation-to-gridrange-and-vice-versa-using-google-apps-script-without-any-scopes/
  const columnLetterToIndex_ = letter => [...letter.toUpperCase()].reduce((c, e, i, a) => (c += (e.charCodeAt(0) - 64) * Math.pow(26, a.length - i - 1)), -1);

  // These values are from your question.
  const conversion = [["B", "B"], ["G", "G"], ["I", "J"], ["J", "K"], ["K", "M"], ["L", "W"], ["M", "X"], ["N", "Y"], ["O", "AA"], ["P", "AC"], ["Q", "AL"], ["R", "AN"], ["S", "AO"], ["T", "AQ"], ["U", "AS"], ["V", "O"], ["W", "P"], ["X", "Q"], ["Y", "U"], ["Z", "AE"], ["AA", "AF"], ["AB", "AG"], ["AC", "AK"], ["AD", "AU"], ["AE", "AV"], ["AF", "AW"], ["AG", "BA"]];

  const converted = conversion.map(e => e.map(f => columnLetterToIndex_(f)));
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const srcSheet = ss.getSheetByName("Payroll");
  const dstSheet = ss.getSheetByName("Log");
  const srcValues = srcSheet.getRange("A3:AG" + srcSheet.getLastRow()).getValues();
  const obj = srcValues.reduce((o, e) => (o[e[0]] = e, o), {});
  const dstRange = dstSheet.getRange("A2:BA" + dstSheet.getLastRow());
  const values = dstRange.getValues().map(e => {
    const ar = obj[e[0]];
    if (ar) {
      converted.forEach(([a, b]) => e[b] = ar[a] || null);
    }
    return e;
  });
  dstRange.setValues(values);
}
  • When this script is run, an array is created and the created array is put into the destination sheet. By this, I thought that the process cost might be able to be reduced a little.

Note:

  • The values of conversion is from your question. If you want to change this, please modify the values.

References:

huangapple
  • 本文由 发表于 2023年4月4日 15:47:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75926763.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定