當我們在 B 列中達到累積值 = 50時,我正在嘗試使用 Google App Script 來拆分此電子表格的 A列。如果累積值不完全匹配,我們需要在我們所在的最后一行停止每個拆分累積值低于 50。我在 C 列中添加了預期的拆分陣列結果。
這是示例電子表格:https ://docs.google.com/spreadsheets/d/1_8ZRTxd64qbxCHrhwDoo4ugWHy7jG1VIKv8hHjtp3Bw/edit#gid=0
最終目標是將每個結果陣列的值存盤在文本檔案中,然后上傳到 Drive 檔案夾中。
在此先感謝您的幫助,
預期 Array1 作為 txt 檔案中的示例
===========================================
編輯,從@Tanaike 腳本,我更新了如下腳本,但沒有運氣:
function test2() {
var raw_values = SpreadsheetApp.getActive().getSheetByName("Sheet1").getRange("A2:J").getValues();
var values = raw_values.map((x) => [x[0], x[2], x[4]])
var destFolderID = "1Qq52QRpYYG_T2AxNWDz0rykZbAGhdpoe";
var fileName = "sample";
createTsv_new(values, destFolderID, fileName)
}
function createTsv_new(values, destFolderID, fileName) {
const folderId = destFolderID; // Please set the folder ID you want to put the created files.
const { res } = values.reduce((o, [s, a, b], i, v) => {
o.tempC = b;
o.tempAr.push([s, a]);
if (o.tempC (v[i 1] ? v[i 1][4] : 0) > 50 || i == v.length - 1) {
o.res.push(o.tempAr);
o.tempAr = [];
o.tempC = 0;
}
return o;
}, { res: [], tempAr: [], tempC: 0 });
if (res.length == 0) return;
res.forEach((e, i) => DriveApp.getFolderById(folderId).createFile(fileName (i 1) ".tsv", e.join("\t")));
}
目前,預期的拆分檔案結果如下:更新后的預期檔案結果
uj5u.com熱心網友回復:
在您的情況下,以下示例腳本怎么樣?
示例腳本:
function myFunction1() {
const folderId = "root"; // Please set the folder ID you want to put the created files.
// 1. Retrieve values from Spreadsheet.
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
const range = sheet.getRange("A1:B" sheet.getLastRow());
const [header, ...values] = range.getValues();
// 2. Create an array including the separated rows.
const { res } = values.reduce((o, [a, b], i, v) => {
o.tempC = b;
o.tempAr.push(a);
// Here, the rows are separated.
if (o.tempC (v[i 1] ? v[i 1][1] : 0) > 50 || i == v.length - 1) {
o.res.push(o.tempAr);
o.tempAr = [];
o.tempC = 0;
}
return o;
}, { res: [], tempAr: [], tempC: 0 });
// 3. Create text files using the created array.
if (res.length == 0) return;
res.forEach((e, i) => DriveApp.getFolderById(folderId).createFile(`Array${i 1}.txt`, [header[0], ...e].join("\n")));
}
- 當為您提供的電子表格運行此腳本時,將從“A”和“B”列中檢索值。并且,創建一個包含分隔行的陣列。并且,使用陣列創建文本檔案。
- 從您的顯示影像中,檔案名類似于
Array1.txt,Array2.txt,,,. - 從您的顯示影像中,標題行被放置到每個文本檔案中。如果您不想包含標題,請修改
[header[0], ...e].join("\n")為e.join("\n").
筆記:
- 此示例腳本適用于您提供的電子表格。因此,當您更改電子表格時,此腳本可能無法使用。請注意這一點。
參考:
- 減少()
- 地圖()
- 創建檔案(名稱,內容)
添加:
關于您的以下第二個新問題,
你能再幫忙嗎,我改變了電子表格中列的配置:docs.google.com/spreadsheets/d/…。我想在每個最終檔案中包含 A 列和 C 列,并且 cumul 現在基于 E 列。最終檔案將是一個 .TSV 檔案。你能再幫忙嗎?
示例腳本如下。
示例腳本:
function myFunction2() {
const folderId = "root"; // Please set the folder ID you want to put the created files.
// 1. Retrieve values from Spreadsheet.
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
const range = sheet.getRange("A1:E" sheet.getLastRow());
const [header, ...values] = range.getValues();
// 2. Create an array including the separated rows.
const { res } = values.reduce((o, [a, , c, , e], i, v) => {
o.tempC = e;
o.tempAr.push([a, c].join("\t"));
// Here, the rows are separated.
if (o.tempC (v[i 1] ? v[i 1][4] : 0) > 50 || i == v.length - 1) {
o.res.push(o.tempAr);
o.tempAr = [];
o.tempC = 0;
}
return o;
}, { res: [], tempAr: [], tempC: 0 });
// 3. Create text files using the created array.
if (res.length == 0) return;
res.forEach((e, i) => DriveApp.getFolderById(folderId).createFile(`Array${i 1}.txt`, [[header[0], header[2]].join("\t"), ...e].join("\n")));
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/463913.html
標籤:谷歌应用脚本
