[英]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:以下是一些额外的问题:
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.