我需要將一些特定列(姓名、年齡、電話)的值從原始作業表獲取并組合到另一張作業表中,但我不知道如何使用谷歌應用程式腳本進行操作。
作業流程如下所示:
- 首先,我只需要從原始表中獲取 Name、Age、Phone 列的值,我不想收集 region 列。原始表看起來像這里的影像:

- 然后我想我必須將它推送到一個陣列并將其粘貼到包含 3 列的結果表中:姓名、年齡、電話
在查詢函式中,我可以使用以下陳述句來完成:
=query("raw sheet","select Col1, Col3, Col4")
但我不知道如何使用谷歌應用程式腳本來做到這一點。
如果你用谷歌應用腳??本來嘗試,請與我分享參考代碼來做,非常感謝!
uj5u.com熱心網友回復:
我相信你的目標如下。
- 您想從源作業表中檢索特定列,并希望使用 Google Apps 腳本將檢索到的列放到目標作業表中。
在這種情況下,以下示例腳本如何?
示例腳本:
請復制并粘貼以下腳本并設定 和 的變數sourceAndDestinationSheet并dstHeader使用腳本編輯器運行該函式。
function myFunction() {
const sourceAndDestinationSheet = ["source sheet name", "destination sheet name"]; // Please set the source sheet name and destination sheet name to the 1st and 2nd element.
const dstHeader = ["Name", "age", "phone"]; // This is the header of the destination values.
// 1. Retrieve values from the source sheet.
const ss = SpreadsheetApp.getActiveSpreadsheet();
const [srcSheet, dstSheet] = sourceAndDestinationSheet.map(s => ss.getSheetByName(s));
const [header, ...values] = srcSheet.getDataRange().getValues();
// 2. Create an array of destination values.
const colIndexes = dstHeader.map(h => header.indexOf(h));
const dstValues = [dstHeader, ...values.map(r => colIndexes.map(i => r[i]))];
// 3. Put the destination values to the destination sheet.
dstSheet.getRange(1, 1, dstValues.length, dstValues[0].length).setValues(dstValues);
}
筆記:
如果您想將此腳本用作自定義函式,那么以下腳本如何?在這種情況下,請將自定義公式
=SAMPLE(Sheet1!A1:D, "Name,age,phone")(假設源作業表和范圍是“Sheet1”和“A1:D”)放到目標作業表中。function SAMPLE(v, h) { const dstHeader = h.split(",").map(e => e.trim()); const [header, ...values] = v; const colIndexes = dstHeader.map(h => header.indexOf(h)); return [dstHeader, ...values.map(r => colIndexes.map(i => r[i]))]; }在此示例腳本中,需要
dstHeader與源表的實際標題名稱相同。請注意這一點。
參考:
- 地圖()
- Google 表格中的自定義函式
uj5u.com熱心網友回復:
嘗試
=query('raw_2'!A1:D,"select A,B,D where A is not null",1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/385322.html
上一篇:getBody()如何從GoogleAppScript中的Gmail作業?
下一篇:java中抽象類和介面的異同點
