英文:
Google Sheets: Removing first 6 lines from cell (data has line breaks and repeated text)
问题
"如您所见,我们的表格中有一些填充了数据的单元格,但在每个单元格的前6行中存在一些重复的数据。我需要将每个单元格中的前6行删除。有没有一种简单的方法可以做到这一点?
我不是一个高级用户,所以我找到的一些解决方案对我来说有些困惑。我尝试过按字符数来删除,但这不是一个一致的数字。我真的只想说“删除前6行”,而不是“删除多少个字符”。我找不到那个具体的解决方案。"
英文:
As you can see, we have some cells filled with data but there is some repeated data in the first 6 lines of every cell. I need those first 6 lines removed in every cell. Is there a way to do this easily enough?
I'm not an advanced user so some of the fixes I've found are a bit confusing for me. I've tried removing by # of characters but that isn't a consistent #. I really just want to say "Remove the first 6 lines" instead of "remove however many characters." I can't find that specific solution.
答案1
得分: 1
点击顶部的扩展,然后选择应用脚本。粘贴以下代码:
function removeFirst6Lines() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var range = sheet.getDataRange();
var values = range.getValues();
for (var i = 0; i < values.length; i++) {
for (var j = 0; j < values[i].length; j++) {
if (typeof values[i][j] === "string") {
var lines = values[i][j].split("\n");
if (lines.length > 6) {
var removedPart = lines.slice(0, 6).join("\n");
var remainingText = lines.slice(6).join("\n");
if (remainingText.startsWith(removedPart)) {
values[i][j] = remainingText.trim();
} else {
// If remaining text doesn't start with the removed part, keep the original value
values[i][j] = values[i][j].trim();
}
} else {
// If the number of lines is 6 or fewer, keep the original value
values[i][j] = values[i][j].trim();
}
}
}
}
range.setValues(values);
}
然后点击运行,它将从每个单元格中删除前6行文本。每次想要使用它时,请导航回应用脚本页面,然后点击运行。
英文:
Click on extensions at the top, then apps script. Paste in this code:
function removeFirst6Lines() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var range = sheet.getDataRange();
var values = range.getValues();
for (var i = 0; i < values.length; i++) {
for (var j = 0; j < values[i].length; j++) {
if (typeof values[i][j] === "string") {
var lines = values[i][j].split("\n");
if (lines.length > 6) {
var removedPart = lines.slice(0, 6).join("\n");
var remainingText = lines.slice(6).join("\n");
if (remainingText.startsWith(removedPart)) {
values[i][j] = remainingText.trim();
} else {
// If remaining text doesn't start with the removed part, keep the original value
values[i][j] = values[i][j].trim();
}
} else {
// If the number of lines is 6 or fewer, keep the original value
values[i][j] = values[i][j].trim();
}
}
}
}
range.setValues(values);
}
Then hit run and it will remove the first 6 lines of text from each cell. Every time you want to use it, navigate back to the apps script page and hit run.
答案2
得分: 1
If the text is in A1, try this formula in B1:
=index(split(substitute(A1,char(10),"|",6),"|"),0,2)
Here is how it works:
- use
substitute()
to replace the 6th occurrence of new-line i.e. Char(10) by pipe i.e. "|" - use
split()
to split the result by pipe - use
index()
to get the 2nd column in the array of value
英文:
If the text is in A1, try this formula in B1:
=index(split(substitute(A1,char(10),"|",6),"|"),0,2)
Here is how it works:
- use
substitute()
to replace the 6th occurrence of new-line i.e. Char(10) by pipe i.e. "|" - use
split()
to split the result by pipe - use
index()
to get the 2nd column in the array of value
答案3
得分: 1
=REGEXREPLACE(A1, "(.*?\n){6}",)
Update
=REGEXREPLACE(A1, "^(.*?(\n|$)){1,6}",,)
英文:
Try this out:
=REGEXREPLACE(A1,"(.*?\n){6}",)
Update
=REGEXREPLACE(A1,"^(.*?(\n|$)){1,6}",)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论