【发布时间】:2021-02-04 21:14:38
【问题描述】:
我想以here 的身份使用 App Scripts 和 Google Sheets 构建联系人数据
完整的应用脚本代码是here
我只需要知道如何添加联系人照片,以便将其上传到表单中,并保存在 Google 表格中(或通过 Google 表格中的链接保存在 Google 云端硬盘中)
我读了this,但它与我想要的不同。
【问题讨论】:
标签: google-apps-script google-sheets
我想以here 的身份使用 App Scripts 和 Google Sheets 构建联系人数据
完整的应用脚本代码是here
我只需要知道如何添加联系人照片,以便将其上传到表单中,并保存在 Google 表格中(或通过 Google 表格中的链接保存在 Google 云端硬盘中)
我读了this,但它与我想要的不同。
【问题讨论】:
标签: google-apps-script google-sheets
我相信你的目标如下。
Form.html添加<input type="file">标签。processForm 的函数。当以上几点反映到你的脚本中时,它变成如下。
Form.html
请修改Form.html如下。
<button type="submit" class="btn btn-primary">Submit</button>
<input class="btn btn-secondary" type="reset" value="Reset">
至:
<div><input type="file" id="file" name="file" accept="image/png,image/jpeg"></div> <!-- Added -->
<button type="submit" class="btn btn-primary">Submit</button>
<input class="btn btn-secondary" type="reset" value="Reset">
JavaScript.html
请修改JavaScript.html如下。
function handleFormSubmit(formObject) {
google.script.run.withSuccessHandler(createTable).processForm(formObject);
document.getElementById("myForm").reset();
}
至:
// I added below function. This is from https://gist.github.com/tanaikech/58d96c023468fc1922d67764251b25e0
const parseValues = async (e) =>
Object.assign(
...(await Promise.all(
[...e].map(
(obj) =>
new Promise(async (res) => {
const temp = {[obj.name]: ""};
if (obj.type == "radio") {
if (obj.checked === true) {
temp[obj.name] = obj.value;
}
} else if (obj.type == "file") {
const files = obj.files;
temp[obj.name] = await (async (files) => {
return await Promise.all(
[...files].map(
(file) =>
new Promise((resolve, reject) => {
const fr = new FileReader();
fr.onload = (f) =>
resolve({
filename: file.name,
mimeType: file.type,
bytes: [...new Int8Array(f.target.result)],
});
fr.onerror = (err) => reject(err);
fr.readAsArrayBuffer(file);
})
)
);
})(files).catch((err) => console.log(err));
} else {
temp[obj.name] = obj.value;
}
res(temp);
})
)
))
);
async function handleFormSubmit(formObject) { // Modified
var obj = await parseValues(formObject); // Added
google.script.run.withSuccessHandler(createTable).processForm(obj); // Modified
document.getElementById("myForm").reset();
}
Code.gs
function processForm(formObject){
if(formObject.RecId && checkID(formObject.RecId)){//Execute if form passes an ID and if is an existing ID
updateData(getFormValues(formObject),globalVariables().spreadsheetId,getRangeByID(formObject.RecId)); // Update Data
}else{ //Execute if form does not pass an ID
appendData(getFormValues(formObject),globalVariables().spreadsheetId,globalVariables().insertRange); //Append Form Data
}
return getLastTenRows();//Return last 10 rows
}
至:
// I added below function.
function saveFileToDrive(e) {
var blob = Utilities.newBlob(e.bytes, e.mimeType, e.filename);
var file = DriveApp.getFolderById("root").createFile(blob); // In this case, the image is saved to the root folder. Please modify `root` to your actulal folder ID.
return file.getDownloadUrl();
}
function processForm(formObject){
var fileLink = formObject.file.length > 0 ? saveFileToDrive(formObject.file[0]) : ""; // Added
if(formObject.RecId && checkID(formObject.RecId)){//Execute if form passes an ID and if is an existing ID
updateData(getFormValues(formObject),globalVariables().spreadsheetId,getRangeByID(formObject.RecId)); // Update Data
}else{ //Execute if form does not pass an ID
appendData(getFormValues(formObject),globalVariables().spreadsheetId,globalVariables().insertRange); //Append Form Data
}
return getLastTenRows();//Return last 10 rows
}
parseValues() 的函数解析,并将解析的对象发送到 Google Apps Script 端。在 Google Apps Script 端,上传图片文件时,图片数据以文件形式保存到 Google Drive,下载链接返回为fileLink。fileLink的取值,请根据自己的实际情况修改脚本使用。I just need to know how can I add the contact PHOTO, so it is getting uploaded in the form, and saved in the Google Sheets (or at Google Drive with link in the Google Sheets)。
【讨论】: