Javascript。展开一串数字

Javascript。展开一串数字

问题描述:

我的脚本位于谷歌电子表格文档中 这是我的脚本:Javascript。展开一串数字

ss = SpreadsheetApp.getActiveSpreadsheet();

function onOpen() { 
var ss = SpreadsheetApp.getActiveSpreadsheet(); 
    var menuEntries = [ {name: "10 Rows", functionName: "TenRows"}, 
         {name: "20 Rows", functionName: "TwentyRows"}, 
         {name: "30 Rows", functionName: "ThirtyRows"}, 
         {name: "40 Rows", functionName: "FortyRows"}, 
         {name: "50 Rows", functionName: "FiftyRows"}]      
    ss.addMenu("Insert Rows", menuEntries); 
} 


function TenRows() { 
    SpreadsheetApp.getActiveSheet().insertRowsBefore(2,10); 
} 

function TwentyRows() { 
    SpreadsheetApp.getActiveSheet().insertRowsBefore(2,20); 
} 

function ThirtyRows() { 
    SpreadsheetApp.getActiveSheet().insertRowsBefore(2,30); 
} 

function FortyRows() { 
    SpreadsheetApp.getActiveSheet().insertRowsBefore(2,40); 
} 

function FiftyRows() { 
    SpreadsheetApp.getActiveSheet().insertRowsBefore(2,50); 
} 

在我的电子表格的C列中,每行都有一串数字。

像这样

C 
7317 
7316 
7315 
7314 
7313 

当我运行我的脚本插入的行数,我怎样才能使它因此它会自动延续了这一上升数,并将其输入到C柱?

感谢

我显然不能给你的代码没有看到整个事情,但我可以给你如何解决这个问题的建议:

  1. 插入新行之前,保存在新行将被插入之后的行中的数字,例如, lastNumber = parseInt(insertRowColumnC.getValue())确保使用parseInt将字符串转换成一个数字(!)
  2. 插入新行
  3. 现在遍历所有从上新插入的行底部
  4. 在你第一次减一lastNumber每一行,然后写它的价值为C列在该行

更新

在看看电子表格我花了几分钟,又上来后W这段代码:

function onEdit(e) { 
    var sheet = e.source.getActiveSheet(); 
    var r = e.source.getActiveRange(); 
    if (r.getColumn() == 3 && r.getRow() == 2) { 
    var value = parseInt(r.getValue()); 
    if (!isNaN(value)) { 
     var next = getFirstRowNumber(sheet); 
     if (value > next) { 
     var count = value - next; 
     sheet.insertRowsAfter(2, count); 
     for(var i = 2; i < 2 + count; i++) { 
      sheet.getRange(i + 1, 3).setValue(value); 
      value--; 
     } 
     r.setValue(''); 

     } else { 
     r.setValue(''); 
     Browser.msgBox('You need to enter a greater ID.'); 
     } 
    } else if (r.getValue() !== '') { 
     r.setValue(''); 
     Browser.msgBox('You need to enter a valid ID.'); 
    } 
    } 
} 

你的问题有点不清楚,但这个工作方式是你想要的。

每当有人在2/C中插入一个新ID时,新ID的缺失ID将自动插入3/C中的ID。

+0

伊沃,无论如何,我可以与你分享文件。我不知道我知道如何实现这一点。 – David 2010-12-04 03:52:19