我正在嘗試重新使用過去在我的作業表中運行良好的腳本,但是當腳本被觸發運行時出現錯誤。錯誤訊息是“自定義函式引數太大”。
const result = range =>
range.flatMap(([a, b, ...v]) => {
const { vv, len } = v.reduce((o, c) => {
const t = typeof c != "string" ? c.toString().split(", ") : c.split(", ");
o.vv.push(t);
o.len = o.len < t.length ? t.length : o.len;
return o;
}, { vv: [], len: 0 });
const temp = vv.map(e => e.concat(Array(len - e.length).fill("")));
return temp[0].map((_, i) => [...(i == 0 ? [a,] : Array(1).fill("")), b, ...temp.map(r => isNaN(r[i].trim()) ? r[i].trim() : r[i].trim() && Number(r[i]))]);
});
// Credits: Tanaike (https://stackoverflow.com/questions/70078195/how-to-split-strings-in-multiple-columns-into-multiple-rows/70081177?noredirect=1#comment123884616_70081177)
似乎發生錯誤是因為我有太多資料行(資料行是 Google 表單回應,到目前為止我有接近 20000 個回應)。有沒有人對我可以嘗試解決這個問題有什么建議?我實際上不熟悉 Google Apps 腳本,如果有人有任何提示,我將不勝感激,謝謝!
uj5u.com熱心網友回復:
在您的情況下,我認為可能需要使用電子表格服務 (SpreadsheetApp) 和/或 Sheets API 而不是自定義函式來放置這些值。那么,下面的示例腳本怎么樣?
模式一:
在此模式中,使用電子表格服務 (SpreadsheetApp) 放置值。請將以下腳本復制并粘貼到電子表格的腳本編輯器中。并且,請設定源表和目標表的表名。并且,sample1使用腳本編輯器運行。
function sample1() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const srcSheet = ss.getSheetByName("Sheet1"); // Please set the source sheet name.
const dstSheet = ss.getSheetByName("Sheet2"); // Please set the destination sheet name.
const values = srcSheet.getDataRange().getValues();
const res = values.flatMap(([a, b, c, d, e, f, g, ...v]) => {
const { vv, len } = v.reduce((o, c) => {
const t = typeof c != "string" ? c.toString().split(",") : c.split(",");
o.vv.push(t);
o.len = o.len < t.length ? t.length : o.len;
return o;
}, { vv: [], len: 0 });
const temp = vv.map(e => e.concat(Array(len - e.length).fill("")));
return temp[0].map((_, i) => [...(i == 0 ? [a, b, c, d] : Array(4).fill("")), e, f, g, ...temp.map(r => isNaN(r[i].trim()) ? r[i].trim() : r[i].trim() && Number(r[i]))]);
});
dstSheet.getRange(1, 1, res.length, res[0].length).setValues(res);
}
模式二:
在此模式中,使用 Sheets API 放置值。請將以下腳本復制并粘貼到電子表格的腳本編輯器中。并且,請設定源表和目標表的表名。并且,請在 Advanced Google services 中啟用 Sheets API。sample1使用腳本編輯器運行。
function sample2() {
const srcSheet = "Sheet1"; // Please set the source sheet name.
const dstSheet = "Sheet2"; // Please set the destination sheet name.
const ss = SpreadsheetApp.getActiveSpreadsheet();
const ssId = ss.getId();
const values = Sheets.Spreadsheets.Values.get(ssId, srcSheet).values;
const res = values.flatMap(([a, b, c, d, e, f, g, ...v]) => {
const { vv, len } = v.reduce((o, c) => {
const t = typeof c != "string" ? c.toString().split(",") : c.split(",");
o.vv.push(t);
o.len = o.len < t.length ? t.length : o.len;
return o;
}, { vv: [], len: 0 });
const temp = vv.map(e => e.concat(Array(len - e.length).fill("")));
return temp[0].map((_, i) => [...(i == 0 ? [a, b, c, d] : Array(4).fill("")), e, f, g, ...temp.map(r => isNaN(r[i].trim()) ? r[i].trim() : r[i].trim() && Number(r[i]))]);
});
Sheets.Spreadsheets.Values.update({ values: res }, ssId, dstSheet, { valueInputOption: "USER_ENTERED" });
}
參考:
- 設定值(值)
- 方法:電子表格.values.update
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/460002.html
