简体   繁体   English

Apps 脚本 Google 表格 - 根据单元格值移动行

[英]Apps Scripts Google Sheets - Move Row based on Cell value

I am not a coder, and trying to manipulate some code (pre-existing/ some added from a partner) but still struggling on small section/ issue.我不是编码员,并试图操纵一些代码(预先存在的/一些从合作伙伴添加的)但仍在小部分/问题上苦苦挣扎。

script (original) written to getRow with active脚本(原版)写到 getRow with active

  // Get the number of the row to move
  var rowToMove = activeSheet.getActiveRange().getRow();
  Logger.log("rowToMove: " + rowToMove);

trying change to have it getRow based on cell value.尝试根据单元格值更改它 getRow。 Have gotten it to where it searches defined range.已经到达它搜索定义范围的位置。 But trying to get it to, based on the value, preform the getRow as it did in the code above... not successful.但是,根据值,试图让它像上面代码中那样执行 getRow ……不成功。

any suggestions welcome, happy to provide more info as well.欢迎任何建议,也很乐意提供更多信息。 电子表格的屏幕截图

function moveRows(){
    Logger.log("moveRows.gs > moveRows");

    // Confirmation check
    var ui = SpreadsheetApp.getUi();
    var response = ui.alert("Move Row", "Confirm Completed Tasks", ui.ButtonSet.YES_NO);
    if (response == ui.Button.YES) {

        // Start: Get the data to move

            // Get the current sheet object
            var activeSheet = SpreadsheetApp.getActiveSheet();
            
            // Get the number of columns
            var lastColumnOfDataInActiveSheet = activeSheet.getLastColumn();
            Logger.log("lastColumnOfDataInActiveSheet: " + lastColumnOfDataInActiveSheet);
    
            **// Get the number of the row to move
            var rowToMove = activeSheet.getRange('E2:E500').getValues();
            rowToMove.forEach( function (r, i) {
              Logger.log(r);
              if (r[0]) 
                activeSheet.getRow();
            }); 
            // Get the range to move
            var rangeToCopyFrom = activeSheet.getRange(rowToMove, 1, 1, lastColumnOfDataInActiveSheet);**

        // End: Get the data to move

        // Start: Copy and paste the data
        
            // Get the target sheet
            var targetSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Completed");
            
            // Get last row in target sheet
            var lastRowInTargetSheet = targetSheet.getLastRow();
            Logger.log("lastRowInTargetSheet: " + lastRowInTargetSheet);
            
            // Get range to paste to
            var rangeToPasteTo = targetSheet.getRange(lastRowInTargetSheet + 1, 1, 1, lastColumnOfDataInActiveSheet);
            
            // Copy and paste the data
            rangeToCopyFrom.copyTo(rangeToPasteTo, {contentsFormat:true});
        
        // End: Copy and paste the data

        // Clear the source data
        rangeToCopyFrom.clearContent();

        // Sort the source sheet
        var lastRowOfDataInActiveSheet = activeSheet.getLastRow();
        var rangeToSort = activeSheet.getRange(2, 1, lastRowOfDataInActiveSheet - 1, lastColumnOfDataInActiveSheet);
        rangeToSort.sort({column: 1, ascending: true});

        // Pop up box tells you it's done
        
    }; // End: Confirmation check

}; // End: moveRows

Address my two commented lines and maybe you can get it to work解决我的两条评论线,也许你可以让它工作

function moveRows(){
    var ui = SpreadsheetApp.getUi();
    var response = ui.alert("Move Row", "Confirm Completed Tasks", ui.ButtonSet.YES_NO);
    if (response == ui.Button.YES) {
            var ash = SpreadsheetApp.getActiveSheet();
            var avs = ash.getRange('E2:E500').getValues();
            avs.forEach( function (r, i) {
              if (r[0]) 
                ash.getRow();//there is no sheet.getRow();
            }); 
            var srg = ash.getRange(avs, 1, 1, ash.getLastColumn());//avs is a two dimensional array of data not a number
            var tsh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Completed");
            var trg = tsh.getRange(tsh.getLastRow() + 1,1);
            srg.copyTo(trg, {contentsFormat:true});
        srg.clearContent();
        var rangeToSort = ash.getRange(2, 1, ash.getLastRow() - 1, ash.getLastColumn());
        rangeToSort.sort({column: 1, ascending: true});
    }; 
}; 

暂无
暂无

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

相关问题 Google表格:根据单元格值将一行数据移动到另一张表格 - Google Sheets: Move a row of data to another sheet based on cell value 根据单元格值更改行对齐方式-Google脚本 - Change Row Alignment Based on Cell Value - Google Scripts Google Sheets Apps Scripts如何在另一个指定列的最后一个非空单元格的同一行中填充特定文本 - Google Sheets Apps Scripts how to populate specific text in the same row of the last non-empty cell in another specified column Google表格根据单元格编号值添加行具有值的单元格计数 - Google Sheets Add row based on cell number value the count of cell which has value 使用 Google 表格的 Apps 脚本,在单元格值更改后插入 Header 行 - Using Apps Script for Google Sheets, Insert a Header Row after change in cell value Google表格将一行移动到当前行中2个单元格条件的另一张表格 - Google Sheets move a row to another sheet on 2 cell criteria in current row Google表格脚本-如何引用给定行的单元格? - Google sheets scripts--how to refer to a cell given a row? 如果单元格以特定文本开头,则删除行 - Google Sheets / Apps Script - Delete Row if cell starts with certain text - Google Sheets / Apps Script Google脚本/表格:如何基于值汇总行/对象? - Google Scripts/Sheets: How to aggregate row/objects based on values? 根据谷歌表格脚本中的唯一 ID 同步行数据 - Sync row data based on unique ID in google sheets scripts
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM