这是一个很大的麻烦。我道歉....
我正在构建一个谷歌工作区附加组件,它允许您:
- 选择一个文件。
- 根据一堆用户输入为该文件设置一个名称。
我遇到了很多问题。
- 一个是当我选择文件时,在它引导我完成授权流程后,它经常在侧边栏中给我一个“内容不适用于此消息”。我不明白为什么会这样。我希望它只显示带有各种用户输入选项的小部件。
- 另一个是我似乎无法记录或记录任何用户输入。您可以看到我尝试在onSearch函数中将它们作为变量调用,然后将它们写入文件名。
如果您需要更多信息来帮助解决问题,请告诉我。我很抱歉代码的混乱。这是谷歌中一个相对较新的功能,我基本上只是从示例插件中改变了代码,所以有一些残留变量等。
// See https://github.com/contributorpw/lodashgs
var _ = LodashGS.load();
/**
* Renders the home page for the add-on. Used in all host apps when
* no context selected.
*
* @param {Object} event - current add-on event
* @return {Card[]} Card(s) to display
*/
function onHomePage(event) {
var card = buildSearchCard_();
return [card];
}
/**
* Renders the contextual interface for a selected Drive file.
*
* @param {Object} event - current add-on event
* @return {Card[]} Card(s) to display
*/
function onDriveItemsSelected(event) {
// For demo, only allow single select on files.
if (event.drive.selectedItems.length != 1) {
var message = "To set a file name, select one file only.";
var card = buildSearchCard_(message);
return [card];
}
var selectedItem = event.drive.selectedItems[0];
if (!selectedItem.addonHasFileScopePermission) {
// Need file access to read ACL, ask user to authorize.
var authorizeFilesAction = CardService.newAction()
.setFunctionName("onAuthorizeDriveFiles")
.setLoadIndicator(CardService.LoadIndicator.SPINNER)
.setParameters({id: selectedItem.id});
var authorizationMessage = CardService.newTextParagraph()
.setText("To change your file name, give permission to the tool to do so.");
var authorizeButton = CardService.newTextButton()
.setText("Authorize")
.setOnClickAction(authorizeFilesAction);
var card = CardService.newCardBuilder()
.addSection(CardService.newCardSection()
.addWidget(authorizationMessage)
.addWidget(authorizeButton))
.build();
return [card];
}}
/**
// Have access, extract ACLs to find co-workers
var emails = extractEmailsFromDrivePermissions_(event);
var people = fetchPeople_(emails);
if (people.length == 0) {
var card = buildSearchCard_("");
return [card];
}
var card = buildTeamListCard_(people)
return [card];
}
/**
* Handles the click for requesting drive file access.
*
* @param {Object} event - current add-on event
* @return {ActionResponse} Request to authorize access to a drive item
*/
function onAuthorizeDriveFiles(event) {
var id = event.parameters.id;
return CardService.newDriveItemsSelectedActionResponseBuilder()
.requestFileScope(id)
.build();
}
/**
* Handles the user search request.
*
* @param {Object} event - current add-on event
* @return {Card[]} Card(s) to display
*/
function handleDateChange(event) {
var dateTimeInput =
event.commonEventObject.formInputs["myDateTimePickerWidgetID"];
var hasDate = dateTimeInput.hasDate;
// The following requires you to configure the add-on to read user locale
// and timezone.
// See https://developers.google.com/gsuite/add-ons/how-tos/access-user-locale
var userTimezoneId = event.userTimezone.id;
// Format and log the date-time selected using the user's timezone.
var formattedDateTime;
if (hasDate) {
formattedDateTime = Utilities.formatDate(
new Date(hasDate), userTimezoneId, "yyy/MM/dd hh:mm:ss");
} else if (hasDate) {
formattedDateTime = Utilities.formatDate(
new Date(hasDate), userTimezoneId, "yyy/MM/dd")
+ ", Time unspecified";
}
if (formattedDateTime) {
console.log(formattedDateTime);
}
}
function onSearch(event) {
if (!event.formInputs || !event.formInputs.query) {
var notification = CardService.newNotification()
.setText("Enter file name components");
return CardService.newActionResponseBuilder()
.setNotification(notification)
.build();
}
var itemId = event.drive.selectedItems[0].id;
var targetFile = DriveApp.getFileById(itemId)
// var query = event.formInputs.query[0];
var dateOnlyPicker =
event.commonEventObject.formInputs.dateOnlyPicker[""].stringInputs.value[0];
var description = event.commonEventObject.formInputs.description[""].stringInputs.value[0];
var department = event.commonEventObject.formInputs.department[""].stringInputs.value[0];
targetFile.setName(dateOnlyPicker +"-"+ description +"-"+ department);
Logger.log(dateOnlyPicker)
Logger.log(description)
Logger.log(department)
// var people = queryPeople_(query);
}
/**
/**
* Builds the search interface for looking up people.
*
* @param {string} opt_error - Optional message to include (typically when
* contextual search failed.)
* @return {Card} Card to display
*/
function buildSearchCard_(opt_error) {
//var banner = CardService.newImage()
//.setImageUrl('https://storage.googleapis.com/gweb-cloudblog-publish/original_images/Workforce_segmentation_1.png');
var dateOnlyPicker = CardService.newDatePicker()
.setTitle("Enter the date.")
.setFieldName("date_field")
// Set default value as May 24 2019. Either a
// number or string is acceptable.
.setValueInMsSinceEpoch(1558668600000)
.setOnChangeAction(CardService.newAction()
.setFunctionName("handleDateChange"));
var description = CardService.newTextInput()
.setFieldName("description")
.setHint("description")
.setTitle("description");
var department = CardService.newSelectionInput()
.setType(CardService.SelectionInputType.DROPDOWN)
.addItem("checkbox one title", "checkbox_one_value", false)
.addItem("checkbox two title", "checkbox_two_value", true)
.addItem("checkbox three title", "checkbox_three_value", true)
.setFieldName("department")
.setTitle("department");
var onSubmitAction = CardService.newAction()
.setFunctionName("onSearch")
.setLoadIndicator(CardService.LoadIndicator.SPINNER);
var submitButton = CardService.newTextButton()
.setText("Set File Name")
.setOnClickAction(onSubmitAction);
var section = CardService.newCardSection()
// .addWidget(banner)
.addWidget(dateOnlyPicker)
.addWidget(description)
.addWidget(department)
.addWidget(submitButton);
if (opt_error) {
var message = CardService.newTextParagraph()
.setText("Note: " + opt_error);
section.addWidget(message);
}
return CardService.newCardBuilder()
.addSection(section)
.build();
}
/**
这是 json 清单,以便您可以对其进行全面测试。
{
"timeZone": "America/New_York",
"dependencies": {
"enabledAdvancedServices": [{
"userSymbol": "Docs",
"serviceId": "docs",
"version": "v1"
}, {
"userSymbol": "Drive",
"serviceId": "drive",
"version": "v2"
}, {
"userSymbol": "Tasks",
"serviceId": "tasks",
"version": "v1"
}, {
"userSymbol": "People",
"serviceId": "peopleapi",
"version": "v1"
}, {
"userSymbol": "Slides",
"serviceId": "slides",
"version": "v1"
}, {
"userSymbol": "AdminDirectory",
"serviceId": "admin",
"version": "directory_v1"
}, {
"userSymbol": "AdminReports",
"serviceId": "admin",
"version": "reports_v1"
}, {
"userSymbol": "Gmail",
"serviceId": "gmail",
"version": "v1"
}, {
"userSymbol": "Sheets",
"serviceId": "sheets",
"version": "v4"
}, {
"userSymbol": "DriveActivity",
"serviceId": "driveactivity",
"version": "v2"
}, {
"userSymbol": "Calendar",
"serviceId": "calendar",
"version": "v3"
}],
"libraries": [{
"userSymbol": "LodashGS",
"libraryId": "1SQ0PlSMwndIuOAgtVJdjxsuXueECtY9OGejVDS37ckSVbMll73EXf2PW",
"version": "5"
}]
},
"exceptionLogging": "STACKDRIVER",
"oauthScopes": ["https://www.googleapis.com/auth/script.locale","https://www.googleapis.com/auth/drive.readonly","https://www.googleapis.com/auth/drive","https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/admin.directory.user.readonly", "https://www.googleapis.com/auth/gmail.addons.execute", "https://www.googleapis.com/auth/gmail.addons.current.message.metadata", "https://www.googleapis.com/auth/calendar.addons.execute", "https://www.googleapis.com/auth/calendar.addons.current.event.read", "https://www.googleapis.com/auth/drive.addons.metadata.readonly", "https://www.googleapis.com/auth/drive.file"],
"urlFetchWhitelist": [],
"runtimeVersion": "DEPRECATED_ES5",
"addOns": {
"common": {
"name": "Set Document Name",
"logoUrl": "https://www.google.com/url?sa=i&url=https%3A%2F%2Fampion.net%2F&psig=AOvVaw28Hqpl6rsudxHTC24KN6Mu&ust=1612506227031000&source=images&cd=vfe&ved=0CAIQjRxqFwoTCOi9pfrLz-4CFQAAAAAdAAAAABAD",
"layoutProperties": {
"primaryColor": "#f24925",
"secondaryColor": "#ea4335"
},
"homepageTrigger": {
"runFunction": "onHomePage",
"enabled": true
},
"universalActions": [{
"label": "Feedback",
"openLink": "https://github.com/googleworkspace/add-ons-samples/issues"
}],
"openLinkUrlPrefixes": ["https://github.com/googleworkspace/add-ons-samples/"]
},
"gmail": {
"contextualTriggers": [{
"unconditional": {
},
"onTriggerFunction": "onGmailMessageSelected"
}]
},
"drive": {
"homepageTrigger": {
"runFunction": "onHomePage",
"enabled": true
},
"onItemsSelectedTrigger": {
"runFunction": "onDriveItemsSelected"
}
},
"calendar": {
"homepageTrigger": {
"runFunction": "onHomePage",
"enabled": true
},
"eventOpenTrigger": {
"runFunction": "onCalendarEventOpen"
},
"currentEventAccess": "READ"
}
}
}
这是日志,根据第一条评论中的第一个建议。我在破译它时也遇到了一些麻烦。
[21-02-04 14:44:15:004 EST] Starting execution
[21-02-04 14:44:15:154 EST] CardService.newDatePicker() [0 seconds]
[21-02-04 14:44:15:154 EST] CardService.DatePicker.setTitle([Enter the date.]) [0 seconds]
[21-02-04 14:44:15:154 EST] CardService.DatePicker.setFieldName([date_field]) [0 seconds]
[21-02-04 14:44:15:154 EST] CardService.DatePicker.setValueInMsSinceEpoch([1.5586686E12]) [0 seconds]
[21-02-04 14:44:15:155 EST] CardService.newAction() [0 seconds]
[21-02-04 14:44:15:155 EST] CardService.Action.setFunctionName([handleDateChange]) [0 seconds]
[21-02-04 14:44:15:155 EST] CardService.DatePicker.setOnChangeAction([Action]) [0 seconds]
[21-02-04 14:44:15:155 EST] CardService.newTextInput() [0 seconds]
[21-02-04 14:44:15:155 EST] CardService.TextInput.setFieldName([description]) [0 seconds]
[21-02-04 14:44:15:155 EST] CardService.TextInput.setHint([description]) [0 seconds]
[21-02-04 14:44:15:155 EST] CardService.TextInput.setTitle([description]) [0 seconds]
[21-02-04 14:44:15:155 EST] CardService.newSelectionInput() [0 seconds]
[21-02-04 14:44:15:155 EST] CardService.SelectionInput.setType([DROPDOWN]) [0 seconds]
[21-02-04 14:44:15:155 EST] CardService.SelectionInput.addItem([checkbox one title, checkbox_one_value, false]) [0 seconds]
[21-02-04 14:44:15:156 EST] CardService.SelectionInput.addItem([checkbox two title, checkbox_two_value, true]) [0 seconds]
[21-02-04 14:44:15:156 EST] CardService.SelectionInput.addItem([checkbox three title, checkbox_three_value, true]) [0 seconds]
[21-02-04 14:44:15:156 EST] CardService.SelectionInput.setFieldName([department]) [0 seconds]
[21-02-04 14:44:15:156 EST] CardService.SelectionInput.setTitle([department]) [0 seconds]
[21-02-04 14:44:15:156 EST] CardService.newAction() [0 seconds]
[21-02-04 14:44:15:156 EST] CardService.Action.setFunctionName([onSearch]) [0 seconds]
[21-02-04 14:44:15:156 EST] CardService.Action.setLoadIndicator([SPINNER]) [0 seconds]
[21-02-04 14:44:15:156 EST] CardService.newTextButton() [0 seconds]
[21-02-04 14:44:15:156 EST] CardService.TextButton.setText([Set File Name]) [0 seconds]
[21-02-04 14:44:15:156 EST] CardService.TextButton.setOnClickAction([Action]) [0 seconds]
[21-02-04 14:44:15:156 EST] CardService.newCardSection() [0 seconds]
[21-02-04 14:44:15:156 EST] CardService.CardSection.addWidget([DatePicker]) [0 seconds]
[21-02-04 14:44:15:157 EST] CardService.CardSection.addWidget([TextInput]) [0 seconds]
[21-02-04 14:44:15:157 EST] CardService.CardSection.addWidget([SelectionInput]) [0 seconds]
[21-02-04 14:44:15:157 EST] CardService.newCardBuilder() [0 seconds]
[21-02-04 14:44:15:157 EST] CardService.CardBuilder.addSection([CardSection]) [0 seconds]
[21-02-04 14:44:15:157 EST] CardService.CardBuilder.build() [0 seconds]
[21-02-04 14:44:15:159 EST] Execution succeeded [0.004 seconds total runtime]