Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/414.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Google Sheets-如果某列的值为0,则删除范围值_Javascript_Google Apps Script_Google Sheets - Fatal编程技术网

Javascript Google Sheets-如果某列的值为0,则删除范围值

Javascript Google Sheets-如果某列的值为0,则删除范围值,javascript,google-apps-script,google-sheets,Javascript,Google Apps Script,Google Sheets,如果在B列中有值为0的单元格,我希望删除B列和C列中的值 我在这个论坛上尝试了基于其他代码的方法,使用push来填充一个空白区域。问题是,当我在两个数组(B和C的值)上清除内容时,它会清除我的所有值,即使它们的值不同于零 我尝试用fontsize搜索我的第二个范围(C值)的错误,结果它成功了 我的代码是: 函数clean0(){ var sheet=SpreadsheetApp.getActiveSheet(); var data=sheet.getRange('B:B').getDisplay

如果在B列中有值为0的单元格,我希望删除B列和C列中的值

我在这个论坛上尝试了基于其他代码的方法,使用push来填充一个空白区域。问题是,当我在两个数组(B和C的值)上清除内容时,它会清除我的所有值,即使它们的值不同于零

我尝试用fontsize搜索我的第二个范围(C值)的错误,结果它成功了

我的代码是:

函数clean0(){
var sheet=SpreadsheetApp.getActiveSheet();
var data=sheet.getRange('B:B').getDisplayValues();
var范围=[];
var Range c=[];
data.forEach(函数(e,i){
如果(e[0]=“0”){
射程.推力(“B”+(i+1));
射程C.push(“C”+(i+1));
}
});
sheet.getRangeList(range.clearContent();
sheet.getRangeList(rangeC.clearContent();

您可以尝试以下编辑的代码,我认为这比使用
推送
功能清除内容要好得多,尽管我没有使用
清除内容
在这种情况下,
===
防止清除范围为空:

function clean0() {
  var sheet = SpreadsheetApp.getActiveSheet(); 
  var range = sheet.getRange('B:D');
  var data = range.getValues(); 
 
  data.forEach(function(row){  
    if (row[0] === 0){
      row[0] ='';
      row[1] ='';
    }  
  });
 range.setValues(data)
}

我认为当列“B”为
0
时,您的脚本可以清除列“B”和“C”中搜索到的行。因此,我想正确理解您当前的问题。但是,不幸的是,我无法理解
我尝试使用fontsize我的第二个范围(C值)搜索错误它是有效的。
。我为我糟糕的英语技能道歉。我能问一下它的细节吗?谢谢你的回答。我想清除范围B:C的值,当B=0中的值清除列C中附加的单元格和反单元格,当我对两个范围使用清除内容时,我的代码会删除所有值,即使它们不同t到0,这就是为什么我尝试将Fontsize与范围C和Fontsize值一起使用的原因。感谢您的回复。现在,我注意到已经发布了两个答案。在这种情况下,我希望尊重它们。我尝试用代码为C的值附加其他空格,但它与该列不起作用。我不知道您如果b列的值为零,则尝试根据b列的值清除b列和c列,让我尝试解决方案多谢Kin Siang,我的函数会清除两列数据(b和c),列“c”包含用户输入的值。列“b”有一个在C中查找值的公式,并在B列中写入一个整数。我尝试使用您的代码添加一个方法来将公式保留在单元格中,但我无法删除“C”中的值我已经按照您的声明修改了代码,应该按照您的期望工作:)谢谢,它真的很成功,非常感谢,我将练习我的编程技能来帮助像您这样的其他人。
function clean0() {
  var sheet = SpreadsheetApp.getActiveSheet(); 
  var range = sheet.getRange('B:C');
  var data = range.getValues();
  var formulas=range.getFormulas();


   data.forEach(function(row, index){    
   //console.log('Index:'+ index + 'row' + row);
    row.forEach(function(amount,index1,array){
  //console.log('Row: ' + array);
    var index2=row.slice(0,1);
    if (row[0] == 0 && row[1]>0){
   
     sheet.getRange(row).clear({commentsOnly: true});

    //console.log('Row after: ' + row)
    //data[index][index1] = "";
    //console.log(data)
        }
        });
      });

   range.setFormulas(formulas)
   //range.setValues(data)
    }