【发布时间】:2023-01-13 00:03:21
【问题描述】:
我在网上找到了一个教程,它允许我将 google sheet 源文件数据转换为 google doc,这非常有用,但是我想改为自动填写 GOOGLE SHEET,试图直接修改编码但不幸的是没有工作,想知道在谷歌表格中设置方差值在技术上是不可能的还是有其他解决方法?
自动填充 google doc 的 youtube 教程: https://www.youtube.com/watch?v=iLALWX0_OYs
以下编码来自教程参考 -
function onOpen() {
const ui = SpreadsheetApp.getUi();
const menu = ui.createMenu('AutoFill Docs');
menu.addItem('Create New Docs', 'createNewGoogleDocs')
menu.addToUi();
}
function createNewGoogleDocs() {
//This value should be the id of your document template that we created in the last step
const googleDocTemplate = DriveApp.getFileById('YOUR_FILE_ID_HERE');
//This value should be the id of the folder where you want your completed documents stored
const destinationFolder = DriveApp.getFolderById('YOUR_FOLDER_ID_HERE')
//Here we store the sheet as a variable
const sheet = SpreadsheetApp
.getActiveSpreadsheet()
.getSheetByName('Data')
//Now we get all of the values as a 2D array
const rows = sheet.getDataRange().getValues();
//Start processing each spreadsheet row
rows.forEach(function(row, index){
//Here we check if this row is the headers, if so we skip it
if (index === 0) return;
//Here we check if a document has already been generated by looking at 'Document Link', if so we skip it
if (row[5]) return;
//Using the row data in a template literal, we make a copy of our template document in our destinationFolder
const copy = googleDocTemplate.makeCopy(`${row[1]}, ${row[0]} Employee Details` , destinationFolder)
//Once we have the copy, we then open it using the DocumentApp
const doc = DocumentApp.openById(copy.getId())
//All of the content lives in the body, so we get that for editing
const body = doc.getBody();
//In this line we do some friendly date formatting, that may or may not work for you locale
const friendlyDate = new Date(row[3]).toLocaleDateString();
//In these lines, we replace our replacement tokens with values from our spreadsheet row
body.replaceText('{{First Name}}', row[0]);
body.replaceText('{{Last Name}}', row[1]);
body.replaceText('{{Position}}', row[2]);
body.replaceText('{{Hire Date}}', friendlyDate);
body.replaceText('{{Hourly Wage}}', row[4]);
//We make our changes permanent by saving and closing the document
doc.saveAndClose();
//Store the url of our new document in a variable
const url = doc.getUrl();
//Write that value back to the 'Document Link' column in the spreadsheet.
sheet.getRange(index + 1, 6).setValue(url)
})
}
我期待编码允许我执行 3 个功能 -
- 运行代码后自动创建 excel
- 源excel中源信息自动填写excel
- 返回 google 电子表格 URL great 并存储在 google source sheet 中
【问题讨论】:
-
我必须为我糟糕的英语水平道歉。来自
I'm expecting the coding allows me to perform 3 functions.,我无法理解您问题中 XLSX 文件和 Google 电子表格之间的关系。我可以问一下你的目标的细节吗?
标签: google-apps-script google-sheets autofill