Google Apps脚本-用弯角替换直引号 [英] Google Apps Script---Replace Straight Quotation Marks with Curly Ones

查看:263
本文介绍了Google Apps脚本-用弯角替换直引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在Google Docs iPad应用上键入小说时,它使用了直接的上下引号,例如:.现在,我想将所有这些引号都更改为卷曲的引号,而不必手动改变它们.

When I was typing a novel on my Google Docs iPad app, it used straight up-and-down quotation marks, like this: ". Now, I want to change all of these quotes to the curly kind, without having to change all of them by hand.

我编写了一个简单的Google Apps脚本文件来解决该问题,但是当我运行它时,它似乎会无限期地显示正在运行函数myFunction ...".

I wrote a simple Google Apps Script file to deal with the issue, but when I run it, it seems to say "Running function myFunction..." indefinitely.

这是我的代码.前几行使用简单的replaceText方法处理句子中间的引号.同时,while语句测试引号前是否有换行符(\ n),并使用该换行符确定是否在引号的开头或结尾.

Here is my code. The first few lines deal with quotes in the middle of the sentence, using a simple replaceText method. Meanwhile, the while statement tests if there is a line break (\n) before the quote, and uses that to determine whether to put a beginning or end quote.

function myFunction() {
  var body = DocumentApp.getActiveDocument().getBody();

  //Replace quotes that are not at beginning or end of paragraph
  body.replaceText(' "', ' "');
  body.replaceText('" ', '" ');

  var bodyString = body.getText();
  var x = bodyString.indexOf('"');
  var bodyText = body.editAsText();

  while (x != -1) {
    var testForLineBreaks = bodyString.slice(x-2, x);
    if (testForLineBreaks == '\n') { //testForLineBreaks determines whether it is the beginning of the paragraph
      //Replace quotes at beginning of paragraph
      bodyText.deleteText(x, x);
      bodyText.insertText(x, '"');
    } else {
      //Replace quotes at end of paragraph
      bodyText.deleteText(x, x);
      bodyText.insertText(x, '"');
    }
    x = bodyString.indexOf('"');
  }
}

我似乎找不到它出了什么问题.更令人困惑的是,当我单击调试器时,它说

I can't seem to find what's wrong with it. And to confuse things more, when I click the debugger, it says

在保存文档之前应用了太多更改.请使用Document.saveAndClose()批量保存更改,然后使用Document.openById()重新打开文档.

Too many changes applied before saving document. Please save changes in smaller batches using Document.saveAndClose(), then reopen the document with Document.openById().

我感谢所有对此的帮助.预先谢谢你!

I appreciate all help with this. Thank you in advance!

推荐答案

some1关于错误消息是正确的,但不幸的是,这并没有解决问题的根源:

some1 was right about the error message, but unfortunately that did not get to the root of the problem:

在while循环结束时,使用变量 bodyString 查找要更改的引号的位置.问题在于bodyString只是一个字符串,因此每次对文档进行更改时我都需要对其进行更新.

At the end of my while loop, the variable bodyString was being used to find the location of quotation marks to change. The problem was that bodyString was just that, a string, and so I needed to update it each time I made a change to the document.

另一个更基本的问题是Google将\ n视为一个字符,因此我不得不将var testForLineBreaks = bodyString.slice(x-2, x);中的参数更改为x-1, x.

Another problem, albeit more basic, was that Google counts \n as one character, so I had to change the parameter in var testForLineBreaks = bodyString.slice(x-2, x); to x-1, x.

解决了这些问题之后,我完成的代码如下:

After tinkering with these issues, my finished code looked like this:

function myFunction() {
  var body = DocumentApp.getActiveDocument().getBody();

  //Replace quotes that are not at beginning or end of paragraph
  body.replaceText(' "', ' "');
  body.replaceText('" ', '" ');

  var bodyString = body.getText();
  var x = bodyString.indexOf('"');
  var bodyText = body.editAsText();

  while (x != -1) {
    var testForLineBreaks = bodyString.slice(x-1, x);

    if (testForLineBreaks == '\n') { //testForLineBreaks determines whether it is the beginning of the paragraph
      //Replace quotes at beginning of paragraph
      bodyText.deleteText(x, x);
      bodyText.insertText(x, '"');
    } else {
      //Replace quotes at end of paragraph
      bodyText.deleteText(x, x);
      bodyText.insertText(x, '"');
    }

    body = DocumentApp.getActiveDocument().getBody();
    bodyString = body.getText();
    x = bodyString.indexOf('"');
    bodyText = body.editAsText();
  }
}

代码还有一个问题.如果引号位于文档的开头(如第一个字符),则脚本将插入错误的引号样式.但是,我打算手动修复它.

There is one remaining problem with the code. If the quote is at the very beginning of the document, as in the first character, then the script will insert the wrong quote style. However, I plan on fixing that manually.

这篇关于Google Apps脚本-用弯角替换直引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆