簡體   English   中英

使用 Google Apps 腳本,如何替換 Google 表格模板中的文本以制作新表格?

[英]Using Google Apps Script, how can I replace text in a Google Sheets template to make a new Sheet?

我有一個由Google Form填充的Google Sheet 我正在使用Google Apps Script添加一些額外的功能。 請隨時根據需要訪問和修改這些內容以提供幫助。

我的目標是能夠從 Google 表單中獲取回復,將其輸入 Google 表格,然后使用模板文件,用回復填充模板,並將其作為 email 附件發送給我指定的任何人。

這是我最終項目的精簡版,但過程應該保持不變,這樣我就可以從這個小規模的例子轉換到那個更大的例子。

目前,在我的Apps Script中,我有能夠成功復制模板文件並相應命名的代碼:

  //Enter collected info into Requirements Template
  const googleSheetTemplate = DriveApp.getFileById('1wqCwMhpuDLReU1hE1CbcDL-Vdw_4zge1xM6oOl34Ohg');
  const destinationFolder = DriveApp.getFolderById('1GxNZQmP8mxHBhVl5AMoqBFs8sAIYzcm3');

  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Form Responses 2');

  const copy = googleSheetTemplate.makeCopy(`${row[3]}, ${row[0]} Vehicle Order` , destinationFolder);
  const newSheet = SpreadsheetApp.openById(copy.getId());
  const A1 = newSheet.getActiveRange();

但是接下來的幾行是為了能夠在新復制的工作表中查找和替換某些字符串,但似乎 function 不正確。

  A1.createTextFinder("{{Customer}}").replaceAllWith(row[3]);
  A1.createTextFinder("{{Car}}").replaceAllWith(row[1]);
  A1.createTextFinder("{{Color}}").replaceAllWith(row[2]);
  A1.createTextFinder("{{Delivery}}").replaceAllWith(row[5]);

我只是在新副本中得到相同的虛擬行。

我正在關注另一篇關於不同問題但目標相同的帖子。 我希望復制的大部分概念來自這篇博文,但他們使用 Google 文檔的地方,我希望使用 Google 表格。 最終,收到這張新表格的人需要用提供的信息做一些計算和事情,所以它仍然需要是表格形式。

我需要進行哪些修改才能成功地將模板中的文本替換為 Google 表單響應中提供的答案?

據我所知,這條線意味着選擇了工作表上的一些單元格:

const A1 = newSheet.getActiveRange();

腳本將僅在這些單元格內搜索和替換。

可能您需要在不參考活動范圍的情況下定義范圍。 是這樣的:

const A1 = newSheet.getRange("A1:A"); // or "A1:Z", or .getDataRange()

我不知道你需要什么范圍。

文檔中, createTextFinder(findText)僅適用於 Sheet class,這意味着您需要在替換文本之前定義一個 Sheet 變量:

  const copy = googleSheetTemplate.makeCopy(`${row[3]}, ${row[0]} Vehicle Order` , destinationFolder);
  const newSheet = SpreadsheetApp.openById(copy.getId()).getSheets()[0];

  newSheet.createTextFinder("{{Customer}}").replaceAllWith(row[3]);
  newSheet.createTextFinder("{{Car}}").replaceAllWith(row[1]);
  newSheet.createTextFinder("{{Color}}").replaceAllWith(row[2]);
  newSheet.createTextFinder("{{Delivery}}").replaceAllWith(row[5]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM