Google Apps脚本以将带有评论的行移至另一工作表。

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

Google Apps script to move rows with comments to another sheet

问题

以下是您提供的代码的中文翻译:

// 我有一个用于将行移动到另一个工作表的脚本,它能够工作,但不是完全按照我希望的方式运行。

function transferData() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sourceSheet = ss.getSheetByName("A PLANIFIER");
  var targetSheet = ss.getSheetByName("MASTER");
  
  // 获取包含'OUI'数据的工作表行号并将其放入数组中
  var ouiData = sourceSheet.getDataRange().getValues().map((x, i) => [i + 1, x[1]]).filter(x => x[1] == 'OUI');
  
  // 获取目标工作表的下一个空行
  var targetSheetNextRow = targetSheet.getRange(1, 8).getDataRegion(SpreadsheetApp.Dimension.ROWS).getLastRow() + 1;
  
  ouiData.forEach(y => {
    var row = y[0];
    
    // 将数据从源工作表复制到目标工作表
    sourceSheet.getRange("B" + row).copyTo(targetSheet.getRange("H" + targetSheetNextRow), {contentsOnly:true});
    sourceSheet.getRange("D" + row).copyTo(targetSheet.getRange("J" + targetSheetNextRow), {contentsOnly:true});
    sourceSheet.getRange("F" + row + ":U" + row).copyTo(targetSheet.getRange("L" + targetSheetNextRow + ":AA" + targetSheetNextRow), {contentsOnly:true});
    sourceSheet.getRange("V" + row + ":AB" + row).copyTo(targetSheet.getRange("AM" + targetSheetNextRow + ":AS" + targetSheetNextRow), {contentsOnly:true});
    sourceSheet.getRange("AE" + row).copyTo(targetSheet.getRange("BL" + targetSheetNextRow), {contentsOnly:true});
    
    // 从源工作表中删除已传输的值
    sourceSheet.getRange("B" + row + ":AE" + row).clearContent();
    
    // 更新目标工作表上的下一个空行
    targetSheetNextRow = targetSheetNextRow + 1;
  });

  // 调用排序函数
  sortM();
  sortAP();
}

这是您提供的代码的中文翻译,只包括代码部分,没有其他内容。如果您需要进一步的帮助,请随时提出。

英文:

I have a script to move rows to another sheet, it works but not completely as I would like.

I have a SpreadSheet with 2 tabs, one "A PLANIFIER" and one "MASTER".
In tab "A PLANIFIER", in column B the value can be OUI or NON, the rows to move are the ones with the value OUI.
Columns B, D, F to U, V to AB and AE from "A PLANIFIER" to H, J, L to AA, AM to AS and BL from "MASTER" respectively.

I modified the script in SputnikDrunk2's reply with the moveTo() function, which works great because it also transfers comments but the downside is that it messes up the conditional formatting of the target sheet. So I used the copyTo() function, which also works very well but the disadvantage here is that the comments are not imported.

I tried several solutions with setComment() and getComment() but I really can't do it.

Could someone please help me understand my mistake?

My Sheet
In script project, the name is : Transfert Client Planifié.gs

My Script:

function transferData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("A PLANIFIER");
var targetSheet = ss.getSheetByName("MASTER");
// Retrieve the sheet row numbers with 'OUI' data & place in an array
var ouiData = sourceSheet.getDataRange().getValues().map((x, i) => [i + 1, x[1]]).filter(x => x[1] == 'OUI');
// Get the next empty row from the target sheet [reference: https://stackoverflow.com/a/56080850/15384825]
var targetSheetNextRow = targetSheet.getRange(1, 8).getDataRegion(SpreadsheetApp.Dimension.ROWS).getLastRow() + 1;
ouiData.forEach(y => {
var row = y[0];
sourceSheet.getRange("B" + row).copyTo(targetSheet.getRange("H" + targetSheetNextRow), {contentsOnly:true});
sourceSheet.getRange("D" + row).copyTo(targetSheet.getRange("J" + targetSheetNextRow), {contentsOnly:true});
sourceSheet.getRange("F" + row + ":U" + row).copyTo(targetSheet.getRange("L" + targetSheetNextRow + ":AA" + targetSheetNextRow), {contentsOnly:true});
sourceSheet.getRange("V" + row + ":AB" + row).copyTo(targetSheet.getRange("AM" + targetSheetNextRow + ":AS" + targetSheetNextRow), {contentsOnly:true});
sourceSheet.getRange("AE" + row).copyTo(targetSheet.getRange("BL" + targetSheetNextRow), {contentsOnly:true});
// Delete transferred values from source sheet
sourceSheet.getRange("B" + row + ":AE" + row).clearContent();
// Update the next empty row on the target sheet
targetSheetNextRow = targetSheetNextRow + 1;
}
)
sortM();
sortAP();
}

I found this solution :

function transferData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("A PLANIFIER");
var targetSheet = ss.getSheetByName("MASTER");
// Retrieve the sheet row numbers with 'OUI' data & place in an array
var ouiData = sourceSheet.getDataRange().getValues().map((x, i) => [i + 1, x[1]]).filter(x => x[1] == 'OUI');
// Get the next empty row from the target sheet [reference: https://stackoverflow.com/a/56080850/15384825]
var targetSheetNextRow = targetSheet.getRange(1, 8).getDataRegion(SpreadsheetApp.Dimension.ROWS).getLastRow() + 1;
ouiData.forEach(y => {
var row = y[0];
sourceSheet.getRange("B" + row).moveTo(targetSheet.getRange("H" + targetSheetNextRow));
sourceSheet.getRange("D" + row).moveTo(targetSheet.getRange("J" + targetSheetNextRow));
sourceSheet.getRange("F" + row + ":U" + row).moveTo(targetSheet.getRange("L" + targetSheetNextRow + ":AA" + targetSheetNextRow));
sourceSheet.getRange("V" + row + ":AB" + row).moveTo(targetSheet.getRange("AM" + targetSheetNextRow + ":AS" + targetSheetNextRow));
sourceSheet.getRange("AE" + row).moveTo(targetSheet.getRange("BL" + targetSheetNextRow));
// Clear format of the target sheet
targetSheet.getRange("H" + targetSheetNextRow + ":BL" + targetSheetNextRow).clearFormat();
// Update the next empty row on the target sheet
targetSheetNextRow = targetSheetNextRow + 1;
// Keep the format of the target line
var rng = targetSheet.getRange("A7:CL7")
rng.copyTo(targetSheet.getRange("A6:CL200" + row), SpreadsheetApp.CopyPasteType.PASTE_FORMAT, false);
rng.copyTo(targetSheet.getRange("A6:CL200" + row), SpreadsheetApp.CopyPasteType.PASTE_CONDITIONAL_FORMATTING, false);
// Keep the format of the source line
var rng = sourceSheet.getRange("A50:AG50")
rng.copyTo(sourceSheet.getRange("A6:AG50" + row), SpreadsheetApp.CopyPasteType.PASTE_FORMAT, false);
rng.copyTo(sourceSheet.getRange("A6:AG50" + row), SpreadsheetApp.CopyPasteType.PASTE_DATA_VALIDATION, false);
}
)
sortM();
sortAP();
}

答案1

得分: 1

建议:请注意,我们不会为您编写代码,但您可以使用下面调整过的脚本作为实际项目的指南。

在检查您的脚本后,您的循环在第一行数据后中断,因此只移动一行。也许您可以尝试使用下面的调整过的脚本,它专门收集了“A PLANIFIER”工作表中的“OUI”数据,并通过forEach()进行循环。我在脚本中添加了一些注释以提供更多上下文信息。

我仍然使用了您的原始逻辑,因为似乎如果使用不同的方法,它可能会破坏整个设置和工作表的格式(而且您已经构建了多个脚本文件)。最后,使用moveTo()复制注释更容易,虽然运行速度会受到影响。

脚本

function transferData() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sourceSheet = ss.getSheetByName("A PLANIFIER");
  var targetSheet = ss.getSheetByName("MASTER");
  // 检索包含'OUI'数据的工作表行号并将其放入数组中
  var ouiData = sourceSheet.getDataRange().getValues().map((x, i) => [i + 1, x[1]]).filter(x => x[1] == 'OUI');
  // 从目标工作表中获取下一个空行[参考:https://stackoverflow.com/a/56080850/15384825]
  var targetSheetNextRow = targetSheet.getRange(1, 8).getDataRegion(SpreadsheetApp.Dimension.ROWS).getLastRow() + 1;

  ouiData.forEach(y => {
    var row = y[0];

    sourceSheet.getRange("B" + row).moveTo(targetSheet.getRange("H" + targetSheetNextRow));
    sourceSheet.getRange("D" + row).moveTo(targetSheet.getRange("J" + targetSheetNextRow));
    sourceSheet.getRange("F" + row + ":U" + row).moveTo(targetSheet.getRange("L" + targetSheetNextRow + ":AA" + targetSheetNextRow));
    sourceSheet.getRange("V" + row + ":AB" + row).moveTo(targetSheet.getRange("AM" + targetSheetNextRow + ":AS" + targetSheetNextRow));
    sourceSheet.getRange("AE" + row).moveTo(targetSheet.getRange("BL" + targetSheetNextRow));

    // 更新目标工作表上的下一个空行
    targetSheetNextRow = targetSheetNextRow + 1;

    // 保留源行的格式
    var rng = sourceSheet.getRange("A50:AG50");
    rng.copyTo(sourceSheet.getRange("A6:AG50" + row), SpreadsheetApp.CopyPasteType.PASTE_FORMAT, false);
  });
  
  sortM();
  sortAP();
}

演示
运行脚本后:
Google Apps脚本以将带有评论的行移至另一工作表。

参考资料

英文:

SUGGESTION

> Note: Please be advised that we do not code for you, but you could use this tweaked script below to guide you in your actual project.

Upon checking on your script, your loop breaks after the first row of data, thus it only moves one row. Perhaps you can test this tweaked script below where it specifically gathers the sheet rows on A PLANIFIER with "OUI" data into an array & loop through it via forEach(). I added some comments on the script for more context.

I still used your original logic as it seems it would break your whole setup & sheet's formatting if a different method is used (also given you've already built multiple script files). Lastly, it is easier to copy the notes with moveTo() for less lines of code though the running speed gets compromised.

Script

function transferData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("A PLANIFIER");
var targetSheet = ss.getSheetByName("MASTER");
// Retrieve the sheet row numbers with 'OUI' data & place in an array
var ouiData = sourceSheet.getDataRange().getValues().map((x, i) => [i + 1, x[1]]).filter(x => x[1] == 'OUI');
// Get the next empty row from the target sheet [reference: https://stackoverflow.com/a/56080850/15384825]
var targetSheetNextRow = targetSheet.getRange(1, 8).getDataRegion(SpreadsheetApp.Dimension.ROWS).getLastRow() + 1;
ouiData.forEach(y => {
var row = y[0];
sourceSheet.getRange("B" + row).moveTo(targetSheet.getRange("H" + targetSheetNextRow));
sourceSheet.getRange("D" + row).moveTo(targetSheet.getRange("J" + targetSheetNextRow));
sourceSheet.getRange("F" + row + ":U" + row).moveTo(targetSheet.getRange("L" + targetSheetNextRow + ":AA" + targetSheetNextRow));
sourceSheet.getRange("V" + row + ":AB" + row).moveTo(targetSheet.getRange("AM" + targetSheetNextRow + ":AS" + targetSheetNextRow));
sourceSheet.getRange("AE" + row).moveTo(targetSheet.getRange("BL" + targetSheetNextRow));
// Update the next empty row on the target sheet
targetSheetNextRow = targetSheetNextRow + 1;
// Keep the format of the source line
var rng = sourceSheet.getRange("A50:AG50")
rng.copyTo(sourceSheet.getRange("A6:AG50" + row), SpreadsheetApp.CopyPasteType.PASTE_FORMAT, false);
}
)
sortM();
sortAP();
}

Demo

After running the script:
Google Apps脚本以将带有评论的行移至另一工作表。

References

huangapple
  • 本文由 发表于 2023年3月31日 02:58:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75892014.html
匿名

发表评论

匿名网友

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

确定