如何从Google工作表中的字符串中移除不需要的字符 [英] How to remove unwanted character from string in Google sheet

查看:109
本文介绍了如何从Google工作表中的字符串中移除不需要的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图清理一些从csv文件导入的数据。我想出了如何获得一些列的格式,我是如何使用它的,但是我仍然在每行的任何一端留下双引号。我假设引用是由 getDataAsString()函数添加的。



正如你所看到的下面我试图使用 replace()来摆脱最后一列中的值的双引号。记录器显示没有的数据,但是我使用的行对表单中的数据没有任何影响。我怀疑这是因为我所做的我到目前为止的脚本是说:显示最后一列没有引号的值,但不实际更新数据。



如何转换显示行到实际更改数据行?

function importFromCSV(){
var file = DriveApp.getFileById(listLatestFile());
// var csvFile = file.getBlob()。getDataAsString(); //获取字符串内容
var csvFile = file.getAs('text / plain')。getDataAsString();
//Logger.log(csvFile);//检查记录器
var csvData = CSVToArray_(csvFile); //转换为二维数组
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(ImpCSVData);
sheet.clear();
sheet.getRange(1,1,csvData.length ,csvD ata [0] .length).setValues(csvData); //一次写入表单
var test = sheet.getRange(1,csvData [0] .length,csvData.length,1).getValue ()。更换(\,);
Logger.log(test);
sheet.getRange(1,4,csvData.length,2).setNumberFormat(DD / MM / YYYY);
sheet.getRange(1,6,csvData.length,1).setNumberFormat(@ STRING @);
sheet.getRange(1,csvData [0] .length,csvData.length,1).getValue()。replace(\,);
}


解决方案


对表格中的数据没有任何影响


在您提供的代码片段中,没有 setValue()或者 setValues()调用正在进行中,如果没有这样做,您不会对电子表格的内容进行任何更改。你有一个变量, text ,你想写入表单,然后你应该这样做:

  sheet.getRange(1,csvData [0] .length,csvData.length,1).setValue(text); 

.getValue()方法返回给定范围内的对象 - 您的代码假定这是一个字符串。不是一个字符串(比如说它是一个日期或数字),那么 .replace()会抛出一个异常。即使它是一个字符串,这行不会使用,因为您不以任何方式存储结果:

  sheet.getRange(1, csvData [0] .length,csvData.length,1).getValue()。replace(\,); 

试试:

  var lastCellRange = sheet.getRange(1,csvData [0] .length,csvData.length,1); 
var text = lastCellRange.getValue()。replace(\,);
lastCellRange.setValue(text);

其他评论:


  • 您的 replace()语句将更改首次出现的双引号字符,而不管它出现在哪里。要替换可能存在或不存在的前导引号或尾随引号,请改为来自这个答案):

    var text = lastCellRange.getValue()。replace(/^\"?(.+?)\"?$/,'$ 1');



I am trying to do a bit of cleaning up on some data that I have imported from a csv file. I figured out how to get the formatting for some of the columns how I need it but am struggling with the double quotes that remain on either end of each row. I assume the quotes are being added as a result of the getDataAsString() function.

As you can see from the below I am trying to use replace() to get rid of the double quote from the values in the last column. The logger shows the data without the ", but the line I have used is not having any effect on the data in the sheet. I suspect this is because all I am doing with the script that I have so far, is saying "show me the value from the last column without the quote" but not actually updating the data.

How can I convert the "show" line to an actual change data line?

function importFromCSV() {
  var file = DriveApp.getFileById(listLatestFile());
  //var csvFile = file.getBlob().getDataAsString();// get string content
  var csvFile = file.getAs('text/plain').getDataAsString();
  //Logger.log(csvFile);// check in the logger
  var csvData = CSVToArray_(csvFile);// convert to 2D array
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var sheet = ss.getSheetByName("ImpCSVData");
      sheet.clear();
      sheet.getRange(1,1, csvData.length, csvData[0].length).setValues(csvData);// write to sheet in one single step
      var test = sheet.getRange(1,csvData[0].length, csvData.length, 1).getValue().replace("\"","");
      Logger.log(test);
      sheet.getRange(1,4, csvData.length, 2).setNumberFormat("DD/MM/YYYY");
      sheet.getRange(1,6, csvData.length, 1).setNumberFormat("@STRING@");
      sheet.getRange(1,csvData[0].length, csvData.length, 1).getValue().replace("\"","");
  }

解决方案

...the line I have used is not having any effect on the data in the sheet

In the code snippet you've provided, there is no setValue() or setValues() call being made. Without that, you aren't making any changes to the content of the spreadsheet. Say you have a variable, text, that you want to write to the sheet. You should then do this:

  sheet.getRange(1,csvData[0].length, csvData.length, 1).setValue(text);

The .getValue() method returns an object from the given range - your code assumes this is a string. If it isn't a string (say it's a date or number instead), then .replace() will throw an exception. Even if it is a string, this line will do nothing of use because you aren't storing the result in any way:

  sheet.getRange(1,csvData[0].length, csvData.length, 1).getValue().replace("\"","");

Try:

  var lastCellRange = sheet.getRange(1,csvData[0].length, csvData.length, 1);
  var text = lastCellRange.getValue().replace("\"","");
  lastCellRange.setValue(text);

Other comments:

  • Your replace() statement will change the first occurrence of the double-quote character, regardless of where it appears. You want to replace leading or trailing quotes which may or may not be there, so try this instead (from this answer):

    var text = lastCellRange.getValue().replace(/^"?(.+?)"?$/,'$1');

这篇关于如何从Google工作表中的字符串中移除不需要的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆