简体   繁体   English

Google 工作表应用程序脚本 - 根据循环条件将数据从一张工作表复制到另一张工作表

[英]Google sheet app script - Copy data from one sheet to another based on condition with loop

I am new to the forum and new to Google App Script.我是论坛的新手,也是 Google App Script 的新手。 I have a sheet with multiple tabs.我有一张带有多个标签的工作表。 I want to do is probably very simple, but seems quite complex to me, coming from VBA...我想做的可能很简单,但对我来说似乎很复杂,来自 VBA...

I would like to: IF Facture(lastrow, 5) = 1 THEN copy-paste DetailDevis(i,1) to DetailFacture(lastrow,3) every time DetailDevis(i,2) = Facture(lastrow,2)我想: IF Facture(lastrow, 5) = 1 THEN 复制粘贴 DetailDevis(i,1) 到 DetailFacture(lastrow,3) 每次 DetailDevis(i,2) = Facture(lastrow,2)

I have made the following, and i would love to get feed back as of why it does not work...我做了以下,我很想得到反馈,为什么它不起作用......

    function onEdit()
{
 var ss = SpreadsheetApp.getActiveSheet()
 var Facture = ss.getSheetByName("FactureDevis") ;
 var FactureDetail = ss.getSheetByName("DetailFactureDevis") ;
 var DevisDetail = ss.getSheetByName("DetailDevis") ;
 var FactureLR = Facture.getLastrow() ;
 var Situ1R = Facture.getRange(FactureLR,5) ;
 var Situ1V = Situ1R.getValues() ;
 var FactureDevisR = Facture.getRange(FactureLR,2) ;
 var FactureDevisV = FactureDevisR.getValues() ;
 var DevisDetailLR = DevisDetail.getLastrow() ;
 var FactureDetailLR = FactureDetail.getLastrow() ;
if (Situ1V == 1)
{
    for (var i = 2 ; i <= DevisDetalLR ; i++) {
 if (DevisDetail.getRange(i,2).getValue() == FactureDevisV)
 {
DevisDetail.getRange(i,1).copyTo(FactureDetail.getRange(FactureDetailLR,3);
}}}

Here are some additional questions:以下是一些额外的问题:

  • Is getLastrow() dynamic? getLastrow() 是动态的吗?
  • Is the place you set your var crucial?您设置 var 的位置是否重要? Or can you set them all at the beginning?或者你可以在开始时设置它们吗?

Try this:尝试这个:

function onEdit(e) {
  var sh1 = e.source.getSheetByName("FactureDevis");
  var sh2 = e.source.getSheetByName("DetailFactureDevis");
  var sh3 = e.source.getSheetByName("DetailDevis");
  var sh1lr = sh1.getLastrow();
  var Situ1V = sh1.getRange(sh1lr, 5).getValue();
  var FactureDevisV = sh1.getRange(sh1lr, 2).getValue();
  var sh3lr = sh3.getLastrow();
  var sh2lr = sh2.getLastrow();
  if (Situ1V == 1) {
    for (var i = 2; i <= sh3lr; i++) {
      if (sh3.getRange(i, 2).getValue() == FactureDevisV) {
        sh3.getRange(i, 1).copyTo(sh2.getRange(sh2lr, 3));
      }
    }
  }
}

sheet.getLastRow() is a function so each time you call it, it recalculates the value. sheet.getLastRow() 是 function 所以每次调用它时,它都会重新计算值。
I create my variables before I need them.我在需要之前创建变量。

I have changed the whole idea.我改变了整个想法。 Instead of copying each row in one sheet and pasting int into another sheet, i have gone for a filtering option:我没有复制一张纸中的每一行并将 int 粘贴到另一张纸中,而是选择了一个过滤选项:

function create_filter(){
    const ss = SpreadsheetApp.getActiveSpreadsheet();
    const s1 = ss.getSheetByName("DetailDevis");
    const s2 = ss.getSheetByName("FactureDevis");
    const s3 = ss.getSheetByName("DetailFactureDevis");
    const s3lr = s3.getLastRow();
    const range = s1.getRange("B:B");
    const datatocheck = s2.getRange(s2.getLastRow(),2).getValue();
    const filter = range.createFilter();
    const Filter_Criteria1 = SpreadsheetApp.newFilterCriteria().whenTextContains(datatocheck);
    const coll1 = 2;
    
    const add_filter1 =  filter.setColumnFilterCriteria(coll1,Filter_Criteria1);

  const range2 = s1.getRange("A2:A");


  range2.copyTo(s3.getRange(s3lr,3));

filter.remove();
}

A new question has arisen now.现在出现了一个新问题。 How can I trigger this function if a row is added to the FactureDevis sheet?如果在 FactureDevis 工作表中添加了一行,如何触发此 function?

Thanks谢谢

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据条件将单元格值从一个Google工作表复制到另一个 - Copy cell values from one google sheet to another based on condition 将数据从一张纸复制到另一张 - Google Script - Copy Data from one sheet to another - Google Script Google 工作表脚本将数据集从一张工作表复制到另一张工作表的顶部(仅限值) - Google sheets script copy data sets from one sheet to another (values only) at the top of the sheet 用于匹配和复制基于另一个相邻单元格的 Google 表格脚本 - Google sheet script to match and copy based from another adjacent cell google脚本:根据触发将公式计算出的值从一张纸复制到另一张纸上(在编辑或时间驱动下) - google script: To copy the value calculated by formula from one sheet to another based on TRIGGER (on edit or time driven) 根据特定的键值将数据从一张纸复制到另一张纸 - Copy data from one sheet to another based on a specific key value 使用 for 循环将数据从一个 Google Sheet 映射到另一个 - Use for loop to map data from one Google Sheet to another one 将范围从一个工作表复制到Google工作表中的另一个工作表 - Copy a range from one sheet to another in google sheet 使用 Google App 脚本将数据从一张工作表复制到另一张工作表时执行缓慢 - Slow Execution for Copying Data from One Sheet to Another Using Google App Script 通过for循环谷歌表格脚本将行复制到另一张表格 - Copy row to another sheet via for loop google sheets script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM