Create Docs through the Drive API with specific page setups (margins, orientation)

夙愿已清 提交于 2019-12-24 12:16:42

问题


My app is already able to create Google Docs through the Drive API, and when doing so I can choose the title, data, and metadata. But I've been looking at the documentation and it seems there is no way to set some other properties of the Docs, such as the margins and the orientation (portrait or landscape) of the Docs being created. Is there a way to do so?

After not finding an answer in the documentation, the only thing I could think of was logging in with my Google Account, going to one of the created Docs, click on "Page setup", then select "Landscape", and finally "Set as default". I thought that by setting it as the default in my Google Account, it may happen that all the Docs created with it may be created as Landscapes. But it didn't work. A possible reason, however, is that the account actually creating the documents is a Service Account, not my regular Google Account. I guess both accounts, while being linked, are not the same thing, so the defaults of one don't apply to the other. Is there some way to set "Portrait" as the default orientation for the Docs created with my Service Account? If not, would the solution be to stop creating the Docs with my Service Account, and start creating them with my regular Google Account, so that the defaults I apply to them apply too to the newly created Docs?


回答1:


Drive API doesn't support Docs related features for you to retrieve/set metadata about the documents.

Service accounts are individual Google accounts and no way related with your own user account. On the other hand, your default settings will only apply to you. You cant programatically set other user's defaults.




回答2:


var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();

//turn landscape if portrait
var oldHeight = body.getPageHeight();
if (oldHeight > body.getPageWidth()){
  body.setPageHeight(body.getPageWidth());
  body.setPageWidth(oldHeight);
}


来源:https://stackoverflow.com/questions/18311101/create-docs-through-the-drive-api-with-specific-page-setups-margins-orientatio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!