简体   繁体   English

根据单元格中的值将行的一部分从一个 Google 电子表格复制到另一个 Google 电子表格?

[英]Copying part of a row from one Google spreadsheet to another google spreadsheet based on a value in a cell?

I am trying to write a script to copy column B:K of a specific row based on if column 11 (K) has a "Y" on it.我正在尝试编写一个脚本来根据第 11 (K) 列是否有“Y”来复制特定行的 B:K 列。

I have scripts that move from one sheet to another within the same spreadsheet that work just fine but I am really struggling with one spreadsheet to another and only moving part of the row instead of the whole thing.我有脚本可以在同一个电子表格中从一张工作表移动到另一张工作表,但我真的很努力将一个电子表格移到另一个电子表格,并且只移动行的一部分而不是整个事情。

Below is the script thus far... would love help!以下是迄今为止的脚本......希望得到帮助!

function onEdit() {
    var sss = SpreadsheetApp.openById("15RWoaW8tRLVGOTDrZvBLJwx0beWEsV3x8pfwGe2zMfc").getSheetByName("2018 2 Week Snapshot for Jim"); // sss = source spreadsheet
    var r = sss.getActiveRange();

    if(sss.getName() == "2018 2 Week Snapshot for Jim" && r.getColumn() == 11 && r.getValue() == "Y") {
        var row = r.getRow();
        var numColumns = sss.getLastColumn();
        var tss = SpreadsheetApp.openById("1dSJOOw_oLVc6Nf-C0MY9715tZGuh69kR6-j_m8EJ-So"); // tss = target spreadsheet
        var targetSheet = tss.getSheetByName("Test Sheet"); // ts = target sheet
        var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
        tss.getRange(row, 1, 1, numColumns).copyTo(target);
    }

}

This is my new code, still struggling:这是我的新代码,仍在挣扎:

function onEdit() {
    var sss = SpreadsheetApp.openById('15RWoaW8tRLVGOTDrZvBLJwx0beWEsV3x8pfwGe2zMfc'); // sss = source spreadsheet
    var ss = sss.getSheetByName('2018 2 Week Snapshot for Jim'); // ss = source sheet
    var SRange = ss.getRange("C:K");

    if(ss.getName() == "2018 2 Week Snapshot for Jim" && SRange.getColumn() == 9 && SRange.getValue() == "y") {
        var row = SRange.getRow();
        var numColumns = ss.getLastColumn();
        var tss = SpreadsheetApp.openById('1dSJOOw_oLVc6Nf-C0MY9715tZGuh69kR6-j_m8EJ-So'); // tss = target spreadsheet
        var ts = tss.getSheetByName('Test Sheet'); // ts = target sheet
        var target = ts.getRange(ts.getLastRow() + 1, 1);
        ss.getRange(row, 1, 1, numColumns).SetValues(target);     
    } 
}
  • When users put y or Y to column K at the sheet of 2018 2 Week Snapshot for Jim on the active spreadsheet, you want to copy only the values of B:K to the last row at the sheet of "Test Sheet" on the spreadsheet of 1dSJOOw_oLVc6Nf-C0MY9715tZGuh69kR6-j_m8EJ-So .当用户将yY放在活动电子表格上2018 2 Week Snapshot for Jim2018 2 Week Snapshot for Jim表的K列时,您只想将B:K的值复制到电子表格上“测试表”工作表的最后一行1dSJOOw_oLVc6Nf-C0MY9715tZGuh69kR6-j_m8EJ-So
    • Spreadsheet of 15RWoaW8tRLVGOTDrZvBLJwx0beWEsV3x8pfwGe2zMfc is the active spreadsheet. 15RWoaW8tRLVGOTDrZvBLJwx0beWEsV3x8pfwGe2zMfc的电子表格是活动电子表格。

If my understanding is correct, how about this modification?如果我的理解是正确的,这个修改怎么样?

Modification poonts:修改点:

  • set of setValues() is lower case. setValues() set是小写的。
  • values of setValues(values) is 2 dimentional array. valuessetValues(values)是2维阵列。
    • The range for setValues(values) is the range for putting the values. setValues(values)的范围是放置值的范围。
  • When you use onEdit() , you can use an event object.当您使用onEdit() ,您可以使用事件对象。

The script which reflected above is as follows.上面反映的脚本如下。

Modified script:修改后的脚本:

function myFunction(e) {
  var sourceSheetName = "2018 2 Week Snapshot for Jim";
  var destinationSpreadsheetId = "1dSJOOw_oLVc6Nf-C0MY9715tZGuh69kR6-j_m8EJ-So";
  var destinationSheetName = "Test Sheet";

  if (e.source.getSheetName() == sourceSheetName && e.range.getA1Notation()[0] == "K" && e.value.toUpperCase() == "Y") {
    var sourceValues = e.source.getRange("B:K").getValues().filter(function(e){return e.filter(String).length > 0});
    var tss = SpreadsheetApp.openById(destinationSpreadsheetId);
    var ts = tss.getSheetByName(destinationSheetName);
    ts.getRange(ts.getLastRow() + 1, 1, sourceValues.length, sourceValues[0].length).setValues(sourceValues);
  }
}

Note :笔记 :

  • Before you use this script, please install myFunction() as an installable trigger.在使用此脚本之前,请安装myFunction()作为可安装的触发器。

References:参考:

If I misunderstand your situation, I'm sorry.如果我误解了你的情况,我很抱歉。

暂无
暂无

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

相关问题 Google 电子表格:根据该行中的单元格值从范围复制行 - Google spreadsheet :Copying row from a range based on a cell value in that row 用于将Google电子表格行的部分内容复制到另一个电子表格并全部基于单元格添加值的脚本 - Script for copying parts of a google spreadsheet row to another spreadsheet and adding a value all based on a cell 如何根据单元格中的值将一行从一个Google电子表格移动到另一个? - How to move a row from one google spreadsheet to another based on a value in a cell? 使用脚本将谷歌电子表格列从一个电子表格复制到另一个电子表格 - Copying google spreadsheet columns from one spreadsheet to another with a script 根据行中单元格中包含的值在Google电子表格中删除一行 - Delete a row in google spreadsheet based on value contained in a cell in row 将数据从一个电子表格复制到另一个Google表格 - Copying data from one spreadsheet to another Google Sheet 将电子表格的一个单元格复制到另一电子表格 - Copying one cell of a spreadsheet to another spreadsheet 如何根据单元格值更改行的背景颜色? -Google电子表格 - How to change background color of row based on cell value? -Google Spreadsheet 如何使用谷歌应用脚本将一行从一个谷歌电子表格复制到另一个谷歌电子表格? - How to copy a row from one google spreadsheet to another google spreadsheet using google apps script? 根据来自另一个特定单元格范围的值从特定范围删除谷歌电子表格中的行 - Delete row in google spreadsheet from a specific range based on values from another specific cell range
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM