【问题标题】:How can I append rows from a sheet to another sheet in GoogleSheets, based on a columns criteria如何根据列标准将工作表中的行附加到 Google 表格中的另一个工作表
【发布时间】:2020-03-30 14:29:21
【问题描述】:

有谁知道如何在 Googlesheets 中将一行从一张纸转置/复制到另一张纸上?

这是我想做的:所以我有一个谷歌电子表格文件。它有 2 个标签/表格。

  • 现在,工作表 1 有一个名为“总计”的列。
  • 现在,我要做的是查看工作表 1 中的“total”列是否为空。
  • 如果它是空的,那么我应该将工作表 1 中具有空列的整行复制到工作表 2。

我知道我必须使用 appendRow() 函数。但这需要硬编码的值。像这样:sheets.appendRow({"a", "b"}) 但我试图附加的行是由用户输入的。

我需要做的是查看哪一行在第 I 列或第 8 列有一个空单元格。

然后将整行复制过来。到目前为止,我能够在 I 列获得空列的行。但我不知道如何附加/复制它。

非常感谢您对此的意见。因为我一直无法找到任何与我需要的东西完全相关的资源。

这是我目前的代码。

 function insertRecord() {
 var sss = SpreadsheetApp.getActiveSpreadsheet().getSheets()[1];
 var values = sss.getDataRange().getValues();
 var getRangeRow2 = sss.getDataRange().getRow()[5];
 var getRangeCol2 = sss.getDataRange().getColumn()[1];
 var placeHolder = sss.insertRowsBefore(5, 1);

 var ss = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];

 var values = ss.getDataRange().getValues();
 var n = 4;
 var c = 8;
 var getRangeRow = ss.getDataRange().getRow()[n];
 var getRangeCol = ss.getDataRange().getColumn()[c];


 for(n=4;n<values.length;n++){  // Loops through all values in column 8
 var column = values[n][c];   // n represents my rows in the getSheets()[0] // number 8 is the column 8.
 var row = values[n]; 
 var col = values[c];
 if(column == null)
 {  // Currently this will get me the rows where the column I has no value in it. This is what its supposed to do. << I am happy with this.
   if(placeHolder)
   {
     placeHolder = ss.getDataRange(getRangeRow, getRangeCol).copyTo(getRangeRow2, getRangeCol2);
   // NOTE: SORRY NOT YELLING. BUT THIS PART IS THE PROBLEM I AM HAVING. IT 
   // WON'T WORK. IT WONT COPY THE ROW TO THE OTHER SHEET.
   }
Logger.log(row, col);
  }
}
}

【问题讨论】:

  • insertRows 是什么以及它与问题有什么关系?见minimal reproducible example
  • 另外,getRow() 返回什么?
  • @TheMaster 好问题。因此,这样做的目的是在页面顶部的第一条记录之前插入一个空行。就像在第 4 行的第一条记录之后一样,我想要一个空行,我可以在其中插入表 1 中的新记录。我希望我没有搞砸这个。同样就 getRow()[5] 而言,工作表 1 上的第一条记录也就是第一个选项卡,从第 5 行开始。所以,我想从该行开始抓取记录。
  • Please refer official documentation. getRow() 返回 Integer 而不是 array。因此不能用[5] 对其进行索引。得到它?任何方式以下答案都应该有效。
  • @TheMaster 如果您将来碰巧使用以下脚本。是的。它确实奏效了。谢谢大家。

标签: google-apps-script google-sheets


【解决方案1】:

追加行不需要硬编码值,您可以从 Sheet1 中获取这些值。 我将继续从您的 Sheet1 中获取所有行并复制 Sheet2 中第 8 列为空的行。 这是一个有效的 sn-p:

function insertRecord() {
   var sheet_2 = SpreadsheetApp.getActiveSpreadsheet().getSheets()[1];

   var sheet_1 = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];

   var rows = sheet_1.getDataRange().getValues(); // Gets the rows with data

   rows
   .filter(row => row[8] == '') // Gets the rows that have the 8th col empty
   .map(row_w_empty8col => sheet_2.appendRow(row_w_empty8col)); // Appends the rows to the second sheet

}

参考资料:

.appendRow(rowContents)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-03
    • 2021-09-21
    • 1970-01-01
    • 1970-01-01
    • 2018-11-21
    • 1970-01-01
    • 1970-01-01
    • 2017-01-14
    相关资源
    最近更新 更多