Seeking a script in Google Sheets that mimics Excel’s replace capability to remove text from ‘tag×’ onward in each cell. For instance:
function stripSuffix() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getDataRange();
var cells = range.getValues();
for (var index = 0; index < cells.length; index++) {
cells[index][0] = cells[index][0].split('tag×')[0].trim();
}
range.setValues(cells);
}
hey, u can use a regex variant: cells[index][0] = cells[index][0].replace(/tag×.*/, ‘’).trim(); its more flexibal and handles variations better, hope it hlps!
A potential alternative that worked well for me involves combining indexOf and substring. I determined if the target text exists and then returned the substring before that point. This method effectively replicates Excel’s replace behavior while handling cases where the marker isn’t found. It also makes the logic transparent because you can see the condition explicitly. This approach yielded consistent results in my project, and I found it easier to adjust if the marker or requirements change over time.