gpt4 book ai didi

javascript - 自动从 Google AppMaker 数据源导出数据

转载 作者:行者123 更新时间:2023-11-29 15:16:07 24 4
gpt4 key购买 nike

有谁知道我们如何根据 Google AppMaker 中数据源中的数据自动生成报告(例如在 12a 生成报告。 m.) 而不是每次用户需要报告时手动单击部署中的导出数据。

我在 Exporting data out of Google AppMaker 上看到过类似的东西但也没有人试图回答这个问题。

如果有人知道如何解决这个问题,我将不胜感激:)

最佳答案

这可以通过使用 Installable Triggers 来实现.

例如,您有一个包含三个字段的学生数据的模型;姓名(字符串)、年龄(数字)和年级(数字)。在服务器脚本上你可以这样写:

//define function to do the data export
function dataExport() {

//create sheet to populate data
var fileName = "Students List " + new Date(); //define file name
var newExport = SpreadsheetApp.create(fileName); // create new spreadsheet
var header = ["Name", "Age", "Grade"]; //define header
newExport.appendRow(header); // append header to spreadsheet

//get all students records
var ds = app.models.students.newQuery();
var allStudents = ds.run();

for(var i=0; i< allStudents.length; i++) {

//get each student data
var student = allStudents[i];
var studentName = student.name;
var studentAge = student.age;
var studentGrade = student.grade;

var newRow = [studentName, studentAge, studentGrade]; //save studen data in a row
newExport.appendRow(newRow); //append student data row to spreadsheet

}

console.log("Finished Exporting Student Data");

}

//invoke function to set up the auto export
function exportData(){

//check if there is an existing trigger for this process
var existingTrigger = PropertiesService.getScriptProperties().getProperty("autoExportTrigger");

//if the trigger already exists, inform user about it
if(existingTrigger) {

return "Auto export is already set";

} else { // if the trigger does not exists, continue to set the trigger to auto export data

//runs the script every day at 1am on the time zone specified
var newTrigger = ScriptApp.newTrigger('dataExport')
.timeBased()
.atHour(1)
.everyDays(1)
.inTimezone("America/Chicago")
.create();

var triggerId = newTrigger.getUniqueId();

if(triggerId) {
PropertiesService.getScriptProperties().setProperty("autoExportTrigger", triggerId);
return "Auto export has been set successfully!";
} else {
return "Failed to set auto export. Try again please";
}

}

}

然后,要删除/停止自动导出,如果需要,您也可以在服务器脚本上编写以下内容:

function deleteTrigger() {

//get the current auto export trigger id
var triggerId = PropertiesService.getScriptProperties().getProperty("autoExportTrigger");

//get all triggers
var allTriggers = ScriptApp.getProjectTriggers();

//loop over all triggers.
for (var i = 0; i < allTriggers.length; i++) {

// If the current trigger is the correct one, delete it.
if (allTriggers[i].getUniqueId() === triggerId) {

ScriptApp.deleteTrigger(allTriggers[i]);
break;

//else delete all the triggers found
} else {

ScriptApp.deleteTrigger(allTriggers[i]);

}

}

PropertiesService.getScriptProperties().deleteProperty("autoExportTrigger");
return "Auto export has been cancelled";

}

希望对您有所帮助!

关于javascript - 自动从 Google AppMaker 数据源导出数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49042255/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com