今天一位網友向我求助,他要在Excel中填寫javascript
PDAA20124527000001
PDAA20124527000002
PDAA20124527000003
java
這樣的序列,但在Excel中沒法智能填充。ide
這個能夠編寫VBA代碼來完成,不過我對VBA不太熟,仍是用JavaScript寫了一個。spa
- <FORM>
- <p><font color="blue" size="+2">序列生成器 v0.0.0001</font></p>
- <p>固定部分:<INPUT TYPE="text" ID="txtFixed" value="PDAA20124527" /></p>
- <p>起始值:<INPUT TYPE="text" ID="txtBegin" value="1"> 結束值:<INPUT TYPE="text" ID="txtEnd" value="999999"> 步長:<INPUT TYPE="text" ID="txtStep" value="1"> <INPUT TYPE="button" VALUE="生成序列" ONCLICK="genCode();"></p>
- </FORM>
- <script language="javascript">
- function genCode()
- {
- sFixed = (document.getElementById("txtFixed")).value;
- if (0==sFixed.length)
- {
- if (!confirm("未指示固定部分!\n要繼續生成嗎?"))
- {
- return;
- }
- }
- iStart = (document.getElementById("txtBegin")).value;
- if (0==iStart.length)
- {
- alert("未指示起始值!");
- return;
- }
- iStart = iStart/1;
- iEnd = (document.getElementById("txtEnd")).value;
- if (0==iEnd.length)
- {
- alert("未指示結束值!");
- return;
- }
- iEnd = iEnd/1;
- iStep = (document.getElementById("txtStep")).value;
- if (0==iStep.length)
- {
- if (!confirm("未指示結束值!\n要默認爲1,繼續生成嗎?"))
- {
- return;
- }
- }
- iStep = iStep/1;
- genSerialCode();
- }
- function genSerialCode()
- {
- document.write('<form name="frmTest">');
- document.write('<TEXTAREA ROWS="30" COLS="90" name="taTest">');
- for (i=iStart; i < iEnd+1; i+=iStep)
- {
- document.write(sFixed);
- var j = i;
- while( (j = j*10) < iEnd )
- {
- //document.writeln("j="+j);
- document.write("0");
- }
- document.writeln(i);
- }
- document.write('<\/TEXTAREA><\/form>');
- var tempval=eval("document.frmTest.taTest");
- tempval.focus();
- tempval.select();
- therange=tempval.createTextRange();
- therange.execCommand("Copy");
- }
- </script>
改進了一下:orm
- <FORM>
- <p><font color="blue" size="+2">序列生成器 v0.0.0001</font></p>
- <p>固定部分:<INPUT TYPE="text" ID="txtFixed" value="PDAA20124527" /></p>
- <p>起始值:<INPUT TYPE="text" ID="txtBegin" value="1"> 結束值:<INPUT TYPE="text" ID="txtEnd" value="999999"> 步長:<INPUT TYPE="text" ID="txtStep" value="1"> <INPUT TYPE="button" VALUE="生成序列" ONCLICK="genCode();"></p>
- </FORM>
- <script language="javascript">
- function genCode()
- {
- sFixed = (document.getElementById("txtFixed")).value;
- if (0==sFixed.length)
- {
- if (!confirm("未指示固定部分!\n要繼續生成嗎?"))
- {
- return;
- }
- }
- iStart = (document.getElementById("txtBegin")).value;
- if (0==iStart.length)
- {
- alert("未指示起始值!");
- return;
- }
- iStart = iStart/1;
- iEnd = (document.getElementById("txtEnd")).value;
- if (0==iEnd.length)
- {
- alert("未指示結束值!");
- return;
- }
- iEnd = iEnd/1;
- iStep = (document.getElementById("txtStep")).value;
- if (0==iStep.length)
- {
- if (!confirm("未指示結束值!\n要默認爲1,繼續生成嗎?"))
- {
- return;
- }
- }
- iStep = iStep/1;
- genSerialCode();
- }
- function genSerialCode()
- {
- document.write('<form name="frmTest">');
- document.write('<TEXTAREA ROWS="30" COLS="90" name="taTest">');
- var j, iMaxLen = String(iEnd).length;
- for (i=iStart; i < iEnd+1; i+=iStep)
- {
- document.write(sFixed);
- var iLen = iMaxLen - String(i).length;
- for (j = 0; j < iLen; j++)
- {
- //document.writeln("j="+j);
- document.write("0");
- }
- document.writeln(i);
- }
- document.write('<\/TEXTAREA><\/form>');
- var tempval=eval("document.frmTest.taTest");
- tempval.focus();
- tempval.select();
- therange=tempval.createTextRange();
- therange.execCommand("Copy");
- }
- </script>