无尽的循环:为什么永无止境? [英] Endless loop : why endless?

查看:86
本文介绍了无尽的循环:为什么永无止境?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图找到一种从文档中删除空白页面的方法,我写了这个脚本很好地完成了这项工作:

 函数remove_blank(){
var Doc = DocumentApp.openById('1ffmPF1iff1ORSPo4XLGyjQGrqVdXx2Py_zza6N_hV3g');
var dd = 1;
var tt ='';
var body = Doc.getActiveSection();
while(body.getNumChildren()> dd){
try {
Logger.log(body.getNumChildren()+''+ dd)
var element = Doc。 getChild(DD);
var type = element.getType();
if(type == DocumentApp.ElementType.TABLE){++ dd}
if(type == DocumentApp.ElementType.PARAGRAPH){
tt = element.getText();
if(tt!='*'){element.removeFromParent(); ++ dd}



编辑:这行必须改变,如下所示:
if(tt!='*'){element.removeFromParent()} else {++ dd}



(不要在删除元素时增加dd,而应该在有效段落上执行)

 } 
}
catch(err)
{
break
}
}
}

但我仍然遇到一个我不明白的问题:当我在具有空白段落和/或分页符的文档上运行该功能以删除一切安好。
如果我在同一个文档(只有段落/表格/段落...)上再次运行它,它会进入无限循环,日志显示var dd 不再增加。
在while循环中的每个表上, dd
增加了,这是怎么发生的?
以下是初始文档内容的日志,以及处理文档之后的日志。
(说实话,我必须说,这种情况可能永远不会发生,因为函数只在另一个进程中被调用,但我很乐意理解;)

  0:段落
1:段落
2:表格
3:段落
4:段落
5:段落
6:表格
7:段落$ b $ 8 8:段落$ b $ 9:段落$ b $ 10:表格$ b 11:段落
12 :段落$ b $ 13:段落
14:表格
15:段落

0:段落
1:表格
2:段落
3:表
4:分段
5:表
6:分段
7:表
8:分段


解决方案

如果我理解正确,dd实质上就是您正在查看的'当前文档元素'。看看下面的代码块:(从你原来的帖子)

  if(type == DocumentApp.ElementType.PARAGRAPH){ 
tt = element.getText();
if(tt!='*'){
element.removeFromParent();
++ dd


code
$ b你只增加dd当
a时)这是一张桌子。
b)这是一段没有文字的段落。



当您的文字段落为文字时,您不会增加dd。所以,当它碰到一个有文本的段落时,dd不会增加,所以它会在循环的下一次运行中再次检查同一段落。因此,让dd一样。



有一个原因,我不确定我的答案。你说它第一次运行良好。 (我假设文档有空和非空段落)。据说,一旦它遇到非空段落,它也应该在第一次运行中卡住。我可能只是想念一些东西。

无论如何,我希望这个答案可能会有所帮助。编辑:我不知道这是否适用,但是如果您的文档中有任何内容

em>不是段落或表格,它也会卡在那里。



编辑#2:没问题,我完全理解你是什么说关于500char限制,以及在遵守这些限制时描述某些事情有多困难:p。



关于此处的代码:

   

我相信while条件每次运行时都会重新评估。这意味着再次调用 body.getNumChildren()(有时)会返回一个较小的数字,因为元素已被删除。



我并不十分确定系统是如何工作的,但我相信例如你有一个列表 [1] [ 2] [3] 。如果你删除 [2] 我怀疑列表会做这样的事情... [1] [3] - > [1] [2] (其中 [3] 变成元素 [2] ) 。正如我所说的,我不是100%确定的,但如果是这样的话,当你移除一个元素时,保持 dd 不变,这可能会起到一定作用。


Trying to find a way to remove blank pages from a document I wrote this script that does the job pretty well :

function remove_blank() {
var Doc = DocumentApp.openById('1ffmPF1iff1ORSPo4XLGyjQGrqVdXx2Py_zza6N_hV3g');
var dd = 1;
var tt='';
var body = Doc.getActiveSection();
while(body.getNumChildren()>dd){
try{
Logger.log(body.getNumChildren()+'  '+dd)
var element = Doc.getChild(dd);
      var type = element.getType();
      if( type == DocumentApp.ElementType.TABLE){++dd}
        if( type == DocumentApp.ElementType.PARAGRAPH ){
          tt=element.getText();
            if(tt!='  *  '){element.removeFromParent();++dd}

EDIT : this line must be changed like this : if(tt!=' * '){element.removeFromParent()}else{++dd}

(don't increment dd when removing element, do it on valid paragraph instead)

       }
    }
    catch(err)
    {
    break
    }
  }
}

But I still get one issue I don't understand : when I run the function on a doc that has empty paragraphs and/or pagebreaks to remove everything is fine. If I run it again on the same doc (that only has paragraphs/table/paragraphs...) It goes in en endless loop and the log shows that var dd doesn't increase anymore after =2. How does that happen since dd is incremented on every table in the while loop ? Below is a log of the initial doc content and, right after, of the 'processed' doc. (To be honest I must say that this situation is probably never going to happen since the function is called just once inside another process but I'd be happy to understand ;-)

0 : PARAGRAPH
1 : PARAGRAPH
2 : TABLE
3 : PARAGRAPH
4 : PARAGRAPH
5 : PARAGRAPH
6 : TABLE
7 : PARAGRAPH
8 : PARAGRAPH
9 : PARAGRAPH
10 : TABLE
11 : PARAGRAPH
12 : PARAGRAPH
13 : PARAGRAPH
14 : TABLE
15 : PARAGRAPH

0 : PARAGRAPH
1 : TABLE
2 : PARAGRAPH
3 : TABLE
4 : PARAGRAPH
5 : TABLE
6 : PARAGRAPH
7 : TABLE
8 : PARAGRAPH

解决方案

If I understand correctly, dd is essentially the 'current document element' that you're looking at. Look at the block of code below: (from your original post)

if( type == DocumentApp.ElementType.PARAGRAPH ){
      tt=element.getText();
      if(tt!='  *  ') {
          element.removeFromParent();
          ++dd
      }
}

You only increment dd when a) It's a table. b) It's a paragraph with no text.

You don't increment dd when it's a paragraph with text. So, when it hits a paragraph that HAS text, dd doesn't increment, so it checks the same paragraph again the next run through the loop. Thus leaving dd the same.

There's one reason that I'm unsure about my answer. You stated that it runs fine the first time through. (I'm assuming the document has both empty and non-empty paragraphs). Supposedly, it should also get stuck in the first run once it hits a non-empty paragraph. I might just be missing something though.

Regardless, I hope this answer will maybe help. If I notice anything else, I'll be sure to edit!

Edit: I don't know if this applies, but if you have anything in your document that isn't a paragraph or table, it will also become stuck there.

Edit#2: No problem, I completely understand what you're saying regarding the 500char limit and how hard it can be to describe certain things while abiding that :p. I just hope I interpreted your comment correctly.

Regarding this code here:

while(body.getNumChildren()>dd) {

I believe that the while condition is re-evaluated each time that it is run. This means that body.getNumChildren() is called again, (sometimes) returning a smaller number because an element has been removed.

I'm not 100% sure about how the system works either, but I believe that for example you have a list, [1] [2] [3]. If you remove [2] I suspect the list will do something like this... [1] [3] -> [1] [2] (where [3] becomes element [2]). As I said, I am not 100% sure about that, but if that is the case, maybe keeping dd constant when you remove an element might do the trick.

这篇关于无尽的循环:为什么永无止境?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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