Google Sheets:从单元格中删除前6行(数据有换行和重复文本)

huangapple go评论87阅读模式
英文:

Google Sheets: Removing first 6 lines from cell (data has line breaks and repeated text)

问题

"如您所见,我们的表格中有一些填充了数据的单元格,但在每个单元格的前6行中存在一些重复的数据。我需要将每个单元格中的前6行删除。有没有一种简单的方法可以做到这一点?

我不是一个高级用户,所以我找到的一些解决方案对我来说有些困惑。我尝试过按字符数来删除,但这不是一个一致的数字。我真的只想说“删除前6行”,而不是“删除多少个字符”。我找不到那个具体的解决方案。"

英文:

Image of Sheet

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 &lt; values.length; i++) {
    for (var j = 0; j &lt; values[i].length; j++) {
        if (typeof values[i][j] === &quot;string&quot;) {
            var lines = values[i][j].split(&quot;\n&quot;);
            if (lines.length &gt; 6) {
                var removedPart = lines.slice(0, 6).join(&quot;\n&quot;);
                var remainingText = lines.slice(6).join(&quot;\n&quot;);
                if (remainingText.startsWith(removedPart)) {
                    values[i][j] = remainingText.trim();
                } else {
                    // If remaining text doesn&#39;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:

  1. use substitute() to replace the 6th occurrence of new-line i.e. Char(10) by pipe i.e. "|"
  2. use split() to split the result by pipe
  3. 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),&quot;|&quot;,6),&quot;|&quot;),0,2)

Here is how it works:

  1. use substitute() to replace the 6th occurrence of new-line i.e. Char(10) by pipe i.e. "|"
  2. use split() to split the result by pipe
  3. 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,&quot;(.*?\n){6}&quot;,)

Update

=REGEXREPLACE(A1,&quot;^(.*?(\n|$)){1,6}&quot;,)

huangapple
  • 本文由 发表于 2023年5月14日 22:56:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76248119.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定