[英]Google Sheets Script - Loop through array, find array items with only text and partial text and replace whole item
I have a code that is half working, the last thing I can't figure out is how to find an array item containing a number (eg 123) and some text (m/s) and replace this whole item with a completely blank array item: []
.我有一个半工作的代码,我最后想不通的是如何找到一个包含数字(例如 123)和一些文本(m/s)的数组项并将整个项替换为一个完全空白的数组项目:
[]
。 No number and no m/s.没有数字,也没有 m/s。
I am not familiar with most of the terminology and am a complete novice at this.我不熟悉大多数术语,并且在这方面完全是新手。 This is what I've got so far:
这是我到目前为止所得到的:
function condForm() {
var sh = ss.getSheetByName('Sheet1');
var range = sh.getRange("D4:D20"); //*** for the sake of brevity setting to 20 instead of 154
var values = range.getValues();
var row = [];
var cols = values[0].length;
for (var i = 0, l = values.length; i < l; i++)
{
row = values[i];
for (var j = 0; j < cols; j++)
{
if (row[j] === 'TTK') { row[j] = ''; }
if(typeof row[j] != "string") { continue; } // line to allow indexOf to work
if (row[j].indexOf('m/s') > -1) { var newValue = row[j].replace('m/s', '') } //attempting to use indexOf to search for partial match to m/s, does not work but does not return error
}
}
Logger.log(values);
}
Without code the logger outputs [[400.0], [600.0], [600.0], [600.0], [], [1283 m/s], [TTK], [440.0], [661.0], [771.0], [771.0], [], [960m/s], [TTK], [381.0], [667.0], [667.0]]
没有代码,记录器输出
[[400.0], [600.0], [600.0], [600.0], [], [1283 m/s], [TTK], [440.0], [661.0], [771.0], [771.0], [], [960m/s], [TTK], [381.0], [667.0], [667.0]]
And with the code above the output becomes [[400.0], [600.0], [600.0], [600.0], [], [1283m/s], [], [440.0], [661.0], [771.0], [771.0], [], [960m/s], [], [381.0], [667.0], [667.0]]
= same number of array items = great success使用上面的代码,output 变为
[[400.0], [600.0], [600.0], [600.0], [], [1283m/s], [], [440.0], [661.0], [771.0], [771.0], [], [960m/s], [], [381.0], [667.0], [667.0]]
= 相同数量的数组项 = 巨大成功
I attempted to use indexOf to search for any array items containing m/s.我试图使用indexOf来搜索任何包含m/s 的数组项。 It didn't work but also didn't return an error.
它没有用,但也没有返回错误。 What's it doing?
它在做什么? Am I on the right lines at all?
我在正确的路线上吗?
I would like a way to turn [1283m/s]
and [960m/s]
into blanks: []
.我想要一种将
[1283m/s]
和[960m/s]
变成空白的方法: []
。
Thank you for reading.感谢您的阅读。
My code was sourced from Max Makhrov: https://stackoverflow.com/a/47575871/1341041我的代码来自 Max Makhrov: https://stackoverflow.com/a/47575871/1341041
and from Andres Duarte : https://stackoverflow.com/a/60226151/1341041来自 Andres Duarte : https://stackoverflow.com/a/60226151/1341041
As far as I can tell you're changing the array row
.据我所知,您正在更改数组
row
。 But you're never changing the array values
.但是您永远不会更改数组
values
。
Probably you need something like this in the middle:中间可能需要这样的东西:
...
if (row[j] === 'TTK') { values[i][j] = ''; }
if (typeof row[j] != "string") { continue; }
if (row[j].indexOf('m/s') > -1) { values[i][j] = row[j].replace('m/s', '') }
...
But actually for your purpose I'd propose to use the native tool TextFinder
:但实际上出于您的目的,我建议使用本机工具
TextFinder
:
function condForm() {
const sheet = ss.getSheetByName('Sheet1');
const range = sheet.getRange('D4:D20');
// range.createTextFinder('m/s').replaceAllWith('');
range.createTextFinder('.*m/s.*').useRegularExpression(true).replaceAllWith('');
range.createTextFinder('^TTK$').useRegularExpression(true).replaceAllWith('');
}
https://developers.google.com/apps-script/reference/spreadsheet/text-finder https://developers.google.com/apps-script/reference/spreadsheet/text-finder
Just in case, if you really into the nested loops it as well can be done a bit cleaner this way:以防万一,如果你真的进入嵌套循环,它也可以通过这种方式做得更干净:
var values = [[400], [600], [600], [600], [], ['1283 m/s'], ['TTK'], [440], [661], [771], [771], [], ['960m/s'], ['TTK'], [381], [667], [667]]; for (row in values) { for (col in values[row]) { if (values[row][col] == 'TTK') values[row][col] = ''; // values[row][col] = values[row][col].toString().replace(/\s*m\/s/g,''); values[row][col] = values[row][col].toString().replace(/.*m\/s.*/,''); } } console.log(values);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.