需要一種方法將陣列加入到 Excel 2010 將視為單獨行的字串中。我試過\n、\r、\r\n、\v、String.fromCharCode(10)、String.fromCharCode(13)。目前它輸出列很好,但行不起作用。您可以提供的任何幫助將不勝感激。我的谷歌搜索得到的最接近的是一個 4 歲的問題,沒有答案。
javascript:(function(){
//values for row 1
var row1 = ['a','b','c'];
//values for row 2
var row2 = ['d','e','f'];
//array of rows
var all_rows = [];
//join with horizontal tab to create columns
all_rows.push(row1.join('\t'));
all_rows.push(row2.join('\t'));
//**not working**, join with * to create row
var d = all_rows.join(String.fromCharCode(10));
//chicanery to put it on the clipboard
var textField = document.createElement('textarea');
textField.innerText = d;
document.body.appendChild(textField);
textField.select();
document.execCommand('copy');
textField.remove();
})();
uj5u.com熱心網友回復:
使用逆向工程;p
使用該資料在 excel 中手動填充行,然后將其復制粘貼到 google chrome 開發控制臺中,將為您生成它發生所需的字串。2022版的excel是:
a\tb\tc\nd\te\tf
帶有的 textfield 方法document.execCommand('copy')實際上使您的選擇錯誤,擺脫了換行符,因此它為什么不起作用。盡管我在下面提出建議,但使用這種方法也許有一些解決方法:
為此,我們有兩種選擇:
- copy('foo') - 僅在 chrome 開發工具的控制臺中可用
- navigator.clipboard.writeText('foo') - 需要對檔案進行聚焦,這意味著用戶需要在網站 ex 上執行任何操作。按鈕點擊
第一個選項
就像打開開發工具并將其粘貼到控制臺一樣簡單:
let row1 = ['a', 'b', 'c'];
let row2 = ['d', 'e', 'f'];
let excelString = row1.join('\t') '\n' row2.join('\t');
copy(excelString)
第二種選擇
出于安全原因,navigator.clipboard.writeText(foo)需要用戶執行任何操作,這意味著我們不能只運行代碼并將其用于復制。我們需要觸發任何動作——簡單的例子就是onclick按鈕上的事件。
<button onclick="copyExcelString()">copy</button>
<script>
let row1 = ['a', 'b', 'c'];
let row2 = ['d', 'e', 'f'];
let excelString = row1.join('\t') '\n' row2.join('\t');
function copyExcelString() {
navigator.clipboard.writeText(excelString).then(
() => {
/* clipboard successfully set */
console.log('copied');
},
() => {
/* clipboard write failed */
console.log('failed copying');
}
);
}
</script>
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/518310.html
