Created
September 13, 2014 13:36
-
-
Save tzmfreedom/f8f4140a4ff453c20b5f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* カスタムオブジェクトGAS__cを作成する。 | |
*/ | |
function createObject() { | |
//package.xml作成 | |
var package = Utilities.newBlob("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\ | |
<Package xmlns=\"http://soap.sforce.com/2006/04/metadata\">\ | |
<types><name>CustomObject</name><members>GAS__c</members>\ | |
</types><version>30.0</version>\ | |
</Package>").setName("hogehoge/package.xml"); | |
//オブジェクトXML作成 | |
var objInfo = Utilities.newBlob("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\ | |
<CustomObject xmlns=\"http://soap.sforce.com/2006/04/metadata\">\ | |
<actionOverrides>\ | |
<actionName>Accept</actionName>\ | |
<type>Default</type>\ | |
</actionOverrides>\ | |
<actionOverrides>\ | |
<actionName>Clone</actionName>\ | |
<type>Default</type>\ | |
</actionOverrides>\ | |
<actionOverrides>\ | |
<actionName>Delete</actionName>\ | |
<type>Default</type>\ | |
</actionOverrides>\ | |
<actionOverrides>\ | |
<actionName>Edit</actionName>\ | |
<type>Default</type>\ | |
</actionOverrides>\ | |
<actionOverrides>\ | |
<actionName>List</actionName>\ | |
<type>Default</type>\ | |
</actionOverrides>\ | |
<actionOverrides>\ | |
<actionName>New</actionName>\ | |
<type>Default</type>\ | |
</actionOverrides>\ | |
<actionOverrides>\ | |
<actionName>Tab</actionName>\ | |
<type>Default</type>\ | |
</actionOverrides>\ | |
<actionOverrides>\ | |
<actionName>View</actionName>\ | |
<type>Default</type>\ | |
</actionOverrides>\ | |
<deploymentStatus>Deployed</deploymentStatus>\ | |
<description>このオブジェクトはGASから作ってます</description>\ | |
<enableActivities>false</enableActivities>\ | |
<enableFeeds>false</enableFeeds>\ | |
<enableHistory>false</enableHistory>\ | |
<enableReports>false</enableReports>\ | |
<label>GoogleAppsScriptから作ったオブジェクト</label>\ | |
<nameField>\ | |
<label>The Name</label>\ | |
<type>Text</type>\ | |
</nameField>\ | |
<searchLayouts/>\ | |
<sharingModel>ReadWrite</sharingModel>\ | |
</CustomObject>").setName("hogehoge/objects/GAS__c.object"); | |
var zip = Utilities.zip([package, objInfo]); | |
var driveZip = DriveApp.createFile(zip); // データをGドライブに格納 | |
var binary = Utilities.base64Encode(zip.getBytes()); | |
var metaBody = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\ | |
<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" \ | |
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \ | |
xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\ | |
<soap:Header><SessionHeader xmlns=\"http://soap.sforce.com/2006/04/metadata\">\ | |
<sessionId>{{sessionId}}</sessionId></SessionHeader></soap:Header><soap:Body>\ | |
<deploy xmlns=\"http://soap.sforce.com/2006/04/metadata\">\ | |
<ZipFile>{{zipfile}}</ZipFile>\ | |
<DeployOptions>\ | |
<allowMissingFiles>false</allowMissingFiles>\ | |
<autoUpdatePackage>false</autoUpdatePackage>\ | |
<checkOnly>false</checkOnly>\ | |
<ignoreWarnings>false</ignoreWarnings>\ | |
<performRetrieve>false</performRetrieve>\ | |
<purgeOnDelete>false</purgeOnDelete>\ | |
<rollbackOnError>false</rollbackOnError>\ | |
<runAllTests>false</runAllTests>\ | |
<singlePackage>false</singlePackage>\ | |
</DeployOptions>\ | |
</deploy>\ | |
</soap:Body></soap:Envelope>"; | |
//deployコール | |
checkAuthorization(); | |
var prop = PropertiesService.getUserProperties(); | |
var sessionInfo = JSON.parse(prop.getProperty("session_info")); | |
var orgId = sessionInfo.id.match(/https:\/\/.+\.com\/id\/([a-zA-Z\d]+)\/([a-zA-Z\d]+)/)[1]; | |
var result = UrlFetchApp.fetch(sessionInfo.instance_url + "/services/Soap/m/30.0/" + orgId, { | |
"method" : "POST", | |
"payload" : metaBody.replace("{{sessionId}}", sessionInfo.access_token).replace("{{zipfile}}", binary), | |
"muteHttpExceptions": true, | |
"headers" : { | |
"SOAPAction" : "\"\"" | |
}, | |
"contentType" : "text/xml" | |
}); | |
//レスポンスのXMLのパース | |
var doc = XmlService.parse(result.getContentText()); | |
var contents = doc.getDescendants(); | |
var checkId = ''; | |
for (var i = 0; i < contents.length; i++ ) { | |
if (contents[i].getType() == XmlService.ContentTypes.ELEMENT) { | |
if (contents[i].asElement().getName() == "id") { | |
var checkId = contents[i].asElement().getText(); | |
break; | |
} | |
} | |
} | |
//ステータスチェック(ポーリング) | |
while(true) { | |
Utilities.sleep(1000); | |
var status = checkDeployStatus(checkId); | |
if (status != "InProgress" && status != "Pending") { | |
break; | |
}; | |
} | |
} | |
/** | |
* ポーリング用メソッド | |
*/ | |
function checkDeployStatus(checkId) { | |
var metaBody = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\ | |
<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" \ | |
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \ | |
xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\ | |
<soap:Header><SessionHeader xmlns=\"http://soap.sforce.com/2006/04/metadata\">\ | |
<sessionId>{{sessionId}}</sessionId></SessionHeader></soap:Header><soap:Body>\ | |
<checkDeployStatus xmlns=\"http://soap.sforce.com/2006/04/metadata\">\ | |
<ID>{{id}}</ID>\ | |
<includeDetails>true</includeDetails>\ | |
</checkDeployStatus>\ | |
</soap:Body></soap:Envelope>"; | |
var prop = PropertiesService.getUserProperties(); | |
var sessionInfo = JSON.parse(prop.getProperty("session_info")); | |
var orgId = sessionInfo.id.match(/https:\/\/.+\.com\/id\/([a-zA-Z\d]+)\/([a-zA-Z\d]+)/)[1]; | |
var result = UrlFetchApp.fetch( | |
sessionInfo.instance_url + "/services/Soap/m/30.0/" + orgId, { | |
"method" : "POST", | |
"payload" : metaBody.replace("{{sessionId}}", sessionInfo.access_token).replace("{{id}}", checkId), | |
"muteHttpExceptions": true, | |
"headers" : { | |
"SOAPAction" : "\"\"" | |
}, | |
"contentType" : "text/xml" | |
}); | |
var doc = XmlService.parse(result.getContentText()); | |
var contents = doc.getDescendants(); | |
var checkId = ''; | |
for (var i = 0; i < contents.length; i++ ) { | |
if (contents[i].getType() == XmlService.ContentTypes.ELEMENT) { | |
if (contents[i].asElement().getName() == "status") { | |
return contents[i].asElement().getText(); | |
} | |
} | |
} | |
return ''; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment