清除Google电子表格中的列内容

时间:2018-07-20 07:41:18

标签: google-apps-script google-sheets

如何清除Google脚本中的列内容而不是删除列?我尝试过几次设置值,但是由于范围是动态的,所以我无法删除列的内容

function onEdit(){

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var activeSheet = ss.getActiveSheet();
  if( activeSheet.getName() == "UAT" ) {
    var activeRange = ss.getActiveRange();
    var activeRow = activeRange.getRow();
    var activeColumn = activeRange.getColumn()
    if( activeColumn >= 3 && activeRow == 70 ) { 
      if ( activeRange.getValue() == "Completed" )
        var coltemp = activeColumn;
        var rowtemp = activeRow;


  var sourcespreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  // set the "Placeholder sheet as active  
  SpreadsheetApp.setActiveSheet(sourcespreadsheet.getSheets()[1]);
  var sourcesheet = SpreadsheetApp.getActiveSheet();

  var targetspreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  // set Master sheet as target
  SpreadsheetApp.setActiveSheet(targetspreadsheet.getSheets()[0]);
  var targetsheet = SpreadsheetApp.getActiveSheet();


  var targetrange = targetsheet.getRange((targetsheet.getLastRow()+1), 1, sourcesheet.getLastRow(), sourcesheet.getLastColumn());
  var rangeValues = sourcesheet.getRange(2, 1, sourcesheet.getLastRow(), sourcesheet.getLastColumn()).getValues();
  targetrange.setValues(rangeValues);

  var sheet = SpreadsheetApp.getActiveSheet();
  activeSheet.hideSheet();

  // set the UAT sheet as active  
  SpreadsheetApp.setActiveSheet(sourcespreadsheet.getSheets()[2]);
  var sourcesheet = SpreadsheetApp.getActiveSheet();

  /* var ss = SpreadsheetApp.getActiveSpreadsheet();
  var activeSheet = ss.getActiveSheet();
  if( activeSheet.getName() == "UAT" ) { 
    var activeRange = ss.getActiveRange();
    var activeRow = activeRange.getRow();
    var activeColumn = activeRange.getColumn()
    if( activeColumn >= 3 && activeRow == 70 ) { // Column EC
      if ( activeRange.getValue() == "Completed" ) */


        activeSheet.deleteColumn( coltemp );

       // i would prefer to delete the values of the column (coltemp )instead of deleting the column

      /*   SpreadsheetApp.getUi().alert('clear column values');
      var rowd = 1;
      for(rowd = 1; rowd <= 200; rowd++)
      {ss.getRange(coltemp+rowd).setValue(rowd);}

      */



    SpreadsheetApp.getUi().alert('thank you for participating in eco survey');
}}}

如何定位动态列并清除内容 我宁愿删除列(coltemp)的值,而不是删除列

1 个答案:

答案 0 :(得分:1)

您要删除coltemp列的内容,该内容可以在脚本中看到。如果我的理解是正确的,那么该修改如何?

修改点:

  • 使用clearContent()删除内容。

修改后的脚本:

请进行如下修改。

来自:
activeSheet.deleteColumn( coltemp );
至 :
activeSheet.getRange(1, coltemp, activeSheet.getLastRow(), 1).clearContent();

注意:

  • 此修改后的脚本会删除列中所有单元格的内容。

参考:

如果这不是您想要的,请告诉我。我想修改它。