今天一位网友向我求助,他要在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>