用于将行移动到新工作表中的不同列的脚本

时间:2017-05-11 21:14:04

标签: javascript google-apps-script google-sheets

我有一个脚本可以根据单元格的值将Google表格行从一张表格移动到另一张表格。我现在要完成的是樱桃挑选栏目放置。

例如,第一列中的“客户”在相应的第一列中从“潜在客户”移至“商机”,第二列中的“客户电话”从商机中的第五列移至第五列,第三列中的“来源”获取全部跳过,然后重复以下列。

有关如何实现这一目标的任何想法?提前谢谢!

以下是移动行的代码。

function onEdit() {
// moves a row from a sheet to another when a magic value is entered in a column

var sheetNameToWatch = "Leads";

var columnNumberToWatch = 9;
var valueToWatch = "Opportunity";
var sheetNameToMoveTheRowTo = "Opportunities";

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getActiveCell();


if (sheet.getName() == sheetNameToWatch && range.getColumn() == columnNumberToWatch && range.getValue() == valueToWatch) {

  var targetSheet = ss.getSheetByName(sheetNameToMoveTheRowTo);
  var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
  sheet.getRange(range.getRow(), 1, 1, sheet.getLastColumn()).moveTo(targetRange);
  sheet.deleteRow(range.getRow());
}
}

1 个答案:

答案 0 :(得分:1)

“对象文字”可用于进行确定来源和目的地的用户设置。

示例:

function onEdit() {
  var mySettings,thisSheet;//Define variables without assigning a value

  // USER INPUT
  mySettings = {//make settings here
    "targetSheetOne":{//First sheet to get data
      "columnToColumnMap":{//source column to be put in target column
        "column1":"column5",//source column1 will be copied to target column5
        "column2":"column2",
        "column3":"column3"
      }
    }
  }

  // END OF USER INPUT

  for (key1 in mySettings) {//Loop through all target sheets in settings
    thisSheet = SpreadsheetApp.getSheetByName(key1);

    innerObject = mySettings[key1];

    for (key2 in innerObject) {
      //Write column data to correct column

    }

  }
}