如何在javascript中乘以空格以在ActiveX FSO Write()方法中使用



例如:

var a = " ";
var b = "";
b = a * 8;
alert(b +"this far to the right");

注意:我不想使用 &nbsp,因为 ActiveX FSO 将用于写入文本文件而不是 html 文件。所以它只需要是空格:

我试图实现的目标的更全面的细节:

我正在尝试让 ActiveX FSO 在提交表单后从 HTML 订单表单写入文本文件,然后继续将订单写入文本文件。文本文件需要采用特定格式,Microsoft Dynamics才能接受作为进口销售。

如下所示,如下所示:客户代码空间 项目代码空间 数量空间

导入.txt减去带有插槽的字符串长度 = 要填充的剩余空间。

C242299A *4 white spaces* 2890 *12 white spaces* 20 *6 white spaces*
[------------][----------------][--------]
12 char slots    16 char slots   8 char slots

write.js将创建此导入.txt文件(这是我需要帮助的部分)

var customercode = document.getElementById("customercode").value;
var itemcode = document.getElementById("itemcode").value;
var quantity = document.getElementById("quantity").value;
var fso = new ActiveXObject("Scripting.FileSystemObject");
var s = fso.OpenTextFile(path+"import.txt",8,true,0);
//customer code string length must be measured to determine remaining spaces 
//to fill before item code can be entered.
//you only have 12 character slots before the next string "item code" can be entered
var remainingSpaces = "";
remainingSpaces = 12 - customercode.length;
spacefill1 = " " * remainingspaces;
remainingSpaces = 16 - itemcode.length;
spacefill2 = " " * remainingSpaces;
remainingSpaces = 8 - quantity.length;
spacefill3 = " " * remainingSpaces;
s.WriteLine(customercode+spacefill1+itemcode+spacefill2+quantity+spacefill3);

应该创建一个如下所示的文本文件:

  C242299A      2890       20

然后将其导入到Microsoft动态。

但问题是它不会将空格相乘,它将空格视为 0/null :(欢迎Jquery解决方案。

要多次重复某个字符,请使用:

var max = 8;//times to repeat
var chr = "a";//char to repeat
console.log(new Array(max + 1).join(chr));//aaaaaaaa

请注意,如果您使用空格执行此操作,它们通常会压缩成一个(但它们就在那里)。

您可以使用 <pre> 标记来显示每个空格(演示)

使用较新的 JS,您可以将String.prototype.repeat()与模板文本一起使用来实现此目的。

const MAX_NUMBER_OF_SPACES = 5;
const EXAMPLE_TEXT = 'Hello World!';
for (let i = 0; i < MAX_NUMBER_OF_SPACES; i++) {
  console.log(`${' '.repeat(i)}${EXAMPLE_TEXT}`);
}

最新更新