gpt4 book ai didi

google-apps-script - 谷歌脚本 : Conditionally copy rows from one spreadsheet to another

转载 作者:行者123 更新时间:2023-12-04 14:38:41 26 4
gpt4 key购买 nike

我找到了基本的解决方案 https://stackoverflow.com/a/4809413/1438650但它缺乏我项目的条件和非连续方面。我需要副本基于 Col G 的值,然后将列 A、B 和 H:S 复制到另一个电子表格。

function movePros() {
var date = Utilities.formatDate(new Date, "CST", "yyyy-MM-dd'T'HH:mm:ss'Z'") //"MM/dd/yyyy"
var source = SpreadsheetApp.getActiveSpreadsheet();
var target = SpreadsheetApp.openById("0ApBdJlRcFy9XdGx0aXRabWlweGxyY3czZzZIemVla3c");
var lastRow = source.getLastRow();
var sheet = source.getSheetByName("City");
var data = sheet.getDataRange.getValues(); //This makes an array, right?
Browser.msgBox(data.slice(1))
// I've tried, but still don't know how get at the individual cell values so that I can both check them and write them in the correct order which is different than the source
var prospect=[DateCalled,PriceSurvey,VolSurvey,Company,Phone,Contact,OdmNotes,Address,City,State,Zip,County,Sic,"","","","","","","","=IFERROR(IF(INDIRECT(\"W\"&ROW())<>\"\",TEXT(INDIRECT(\"W\"&ROW()),\"yyyyww\")+0,IF(INDIRECT(\"T\"&ROW())<>\"\",TEXT(INDIRECT(\"T\"&ROW()),\"yyyyww\")+0,IF(INDIRECT(\"A\"&ROW())<>\"\",TEXT(INDIRECT(\"A\"&ROW()),\"yyyyww\")+1,\"\"))),\"\")","U","","","","","",ODM,"",""];
// var prospect=[data()[0]..... ugh, I don/'t know....
/*
var ODM = sheet.getRange("A"+i).getValues();
var DateCalled = sheet.getRange("B"+i).getValues();
var PriceSurvey = sheet.getRange("H"+i).getValues();
var VolSurvey = sheet.getRange("I"+i).getValues();
var Company = sheet.getRange("J"+i).getValues();
var Phone = sheet.getRange("K1"+i).getValues();
var Contact = sheet.getRange("L"+i).getValues();
var OdmNotes = sheet.getRange("M"+i).getValues();
var Address = sheet.getRange("N"+i).getValues();
var City = sheet.getRange("O"+i).getValues();
var State = sheet.getRange("P"+i).getValues();
var Zip = sheet.getRange("Q"+i).getValues();
var County = sheet.getRange("R"+i).getValues();
var Sic = sheet.getRange("S"+i).getValues();
*/

while (i <= lastRow) {
var i = 2
if (sheet.getRange("G"+i).getValue()==1 && sheet.getRange("U"+i).getValue()=="") { //cheking

target.appendRow(prospect);

source.getRange("U"+i).setValue(date);
i++;

} else {
i++;
}
}


}

最佳答案

这是一个脚本“提案”,您可以完成它以使其生效。需要实现“if”语句,如果不确切知道您需要什么,我无法做到这一点。在那里使用数组的一般想法,我相信你会设法让它发挥作用。

function movePros() {
var date = Utilities.formatDate(new Date, "CST", "yyyy-MM-dd'T'HH:mm:ss'Z'") //"MM/dd/yyyy"
var source = SpreadsheetApp.getActiveSpreadsheet();
var target = SpreadsheetApp.openById("0ApBdJlRcFy9XdGx0aXRabWlweGxyY3czZzZIemVla3c");
var lastRow = source.getLastRow();
var sheet = source.getSheetByName("City");
var data = sheet.getDataRange().getValues();
var prospect=['ODM','DateCalled','PriceSurvey','VolSurvey','Company','Phone','Contact','OdmNotes','Address','City,State','Zip','County','Sic']
var line = new Array();
var targetArray= new Array();
targetArray.push(prospect);// create a header on first row of target
for(i=0;i<data.length;++i){ // iterate rows
for (j=0;j<data[i].length;++j){ // iterate columns
if (place here your conditions if data must be copied or not, you can have many conditions alltogether){line.push(data[i][j]}// if cell meets condition, add to destination row
}
targetArray.push(line)// add row to destination (you could need to add a condition here too)
}
target.getRange(1,1,targetArray.length,targetArray[0].length).setValues(targetArray);
}

备注:data[i] 是源工作表的一行,data[i][j] 是这一行中的一个单元格,从那里您可以访问任何行中的任何单元格。人们通常在 for 循环中执行此操作,但您可以在不循环每一行的情况下执行此操作以获取所需的数据。

编辑#1:复制非连续数据的条件可能是这样的:

  if (j==5||j==7||j==9){line.push(data[i][j])}

抱歉有点长。编辑#2:查看你的目标文档,我注意到你有多个工作表,你还应该告诉脚本它应该在哪个工作表上写入数据。这是一个如何做到这一点的例子:

  var ss = SpreadsheetApp.openById("your spreadsheet ID");// this is already done in your script
SpreadsheetApp.setActiveSpreadsheet(ss);
var sh = SpreadsheetApp.setActiveSheet(ss.getSheets()[0]);// select first sheet in target spreadsheet

关于google-apps-script - 谷歌脚本 : Conditionally copy rows from one spreadsheet to another,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10907267/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com