Reputation: 125
I`m trying to replace old values with new values using setValues in Google sheet script.
The data is in the below link...
https://docs.google.com/spreadsheets/d/1pSUVkxM9FhSNgizedHbY2MnYGTnC2iiYLfrWsoPmDks/edit?usp=sharing
I`m basically trying to remove first 14 characters and the last 12 characters under "Tracker" column
Below is the code I tried..
function URLReplacement() {
var ss = SpreadsheetApp.getActive().getSheetByName("transformer");
var rng = ss.getRange("G:G");
var data = rng.getValues();
for (var items in data)
{
var newer = data[items][0].substring(14)
// Turn these strings into an array
var newerr = newer.split(" ")
// Turn this into 2 dimensional array to use setValues
ss.getRange("G:G").setValues([newerr])
Logger.log([newer]);
}
}
But now, I get errors with the setValues statement Saying the range I set there do not match the data
What am I doing wrong here..?
Can anyone please provide me with suggestions / advice?
Upvotes: 1
Views: 452
Reputation: 201378
IMAGE_SUFFIX_"http://google.com"<xxxnouse>"
to http://google.com
at the column "G".IMAGE_SUFFIX_"http://google.com"<xxxnouse>"
is constant.If my understanding is correct, how about this modification? The reason of your error is that [newer]
is not 2 dimensional array for using setValues()
. If this error was removed, the header is removed by overwriting the empty value. So I would like to modify as follows.
getLastRow()
is used, the data size retrieved by it can be reduced from that retrieved by "G:G". By this, the process cost can be reduced.getRange(2, 7, ss.getLastRow(), 1)
.IMAGE_SUFFIX_"http://google.com"<xxxnouse>"
, split()
was used for parsing this value.setValues()
. By this, the process cost can be also reduced.function URLReplacement() {
var ss = SpreadsheetApp.getActive().getSheetByName("transformer");
var rng = ss.getRange(2, 7, ss.getLastRow(), 1); // Modified
var data = rng.getValues();
var convertedData = data.map(function(e) {return e[0] ? [e[0].split('"')[1]] : e}); // Added
rng.setValues(convertedData); // Added
}
If this was not the result you want, I apologize.
Upvotes: 1